home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gcc258s.zoo / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-30  |  121.7 KB  |  4,586 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. /* This program is the user interface to the C compiler and possibly to
  24. other compilers.  It is used because compilation is a complicated procedure
  25. which involves running several programs and passing temporary files between
  26. them, forwarding the users switches to those programs selectively,
  27. and deleting the temporary files at the end.
  28.  
  29. CC recognizes how to compile each input file by suffixes in the file names.
  30. Once it knows which kind of compilation to perform, the procedure for
  31. compilation is specified by a string called a "spec".  */
  32.  
  33. #ifdef CROSSATARI
  34. #ifdef atarist
  35. #undef atarist
  36. #endif
  37. #ifdef atariminix
  38. #undef atariminix
  39. #endif
  40. #endif
  41.   
  42. #include <sys/types.h>
  43. #include <ctype.h>
  44. #include <signal.h>
  45. #include <sys/stat.h>
  46. #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
  47.  
  48. #include "config.h"
  49. #include "obstack.h"
  50. #include "gvarargs.h"
  51. #include <stdio.h>
  52. #include <string.h>
  53.     /* if your supplier still does'nt have string.h, shoot him.. */
  54.  
  55. #ifndef R_OK
  56. #define R_OK 4
  57. #define W_OK 2
  58. #define X_OK 1
  59. #endif
  60.  
  61. /* Define a generic NULL if one hasn't already been defined.  */
  62.  
  63. #ifndef NULL
  64. #define NULL 0
  65. #endif
  66.  
  67. #ifndef GENERIC_PTR
  68. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  69. #define GENERIC_PTR void *
  70. #else
  71. #define GENERIC_PTR char *
  72. #endif
  73. #endif
  74.  
  75. #ifndef NULL_PTR
  76. #define NULL_PTR ((GENERIC_PTR)0)
  77. #endif
  78.  
  79. #if (defined(USG) || defined(atariminix))
  80. #define vfork fork
  81. #endif /* USG */
  82.  
  83. /* On MSDOS, write temp files in current dir
  84.    because there's no place else we can expect to use.  */
  85. #if __MSDOS__
  86. #ifndef P_tmpdir
  87. #define P_tmpdir "."
  88. #endif
  89. #endif
  90.  
  91. /* Test if something is a normal file.  */
  92. #ifndef S_ISREG
  93. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  94. #endif
  95.  
  96. /* Test if something is a directory.  */
  97. #ifndef S_ISDIR
  98. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  99. #endif
  100.  
  101. /* By default there is no special suffix for executables.  */
  102. #ifndef EXECUTABLE_SUFFIX
  103. #define EXECUTABLE_SUFFIX ""
  104. #endif
  105.  
  106. /* By default, colon separates directories in a path.  */
  107. #ifndef PATH_SEPARATOR
  108. #define PATH_SEPARATOR ':'
  109. #endif
  110.  
  111. #ifdef CROSSATARI
  112. #include <ctype.h>
  113. #endif
  114.  
  115. #ifdef atarist
  116. #include <osbind.h>
  117. #include <ctype.h>
  118. #include <fcntl.h>        /* fgth, for redirection of stderr */
  119. long _stksize = 8192;
  120. #endif
  121.  
  122. #define obstack_chunk_alloc xmalloc
  123. #define obstack_chunk_free free
  124.  
  125. extern void free ();
  126. extern char *getenv ();
  127.  
  128. extern int errno, sys_nerr;
  129. #if defined(bsd4_4)
  130. extern const char *const sys_errlist[];
  131. #else
  132. extern char *sys_errlist[];
  133. #endif
  134.  
  135. extern int execv (), execvp ();
  136.  
  137. /* If a stage of compilation returns an exit status >= 1,
  138.    compilation of that file ceases.  */
  139.  
  140. #define MIN_FATAL_STATUS 1
  141.  
  142. /* Flag saying to print the full filename of libgcc.a
  143.    as found through our usual search mechanism.  */
  144.  
  145. static int print_libgcc_file_name;
  146.  
  147. /* Flag indicating whether we should print the command and arguments */
  148.  
  149. static int verbose_flag;
  150.  
  151. /* Nonzero means write "temp" files in source directory
  152.    and use the source file's name in them, and don't delete them.  */
  153.  
  154. static int save_temps_flag;
  155.  
  156. /* The compiler version specified with -V */
  157.  
  158. static char *spec_version;
  159.  
  160. /* The target machine specified with -b.  */
  161.  
  162. static char *spec_machine = DEFAULT_TARGET_MACHINE;
  163.  
  164. /* Nonzero if cross-compiling.
  165.    When -b is used, the value comes from the `specs' file.  */
  166.  
  167. #ifdef CROSS_COMPILE
  168. static int cross_compile = 1;
  169. #else
  170. static int cross_compile = 0;
  171. #endif
  172.  
  173. /* The number of errors that have occurred; the link phase will not be
  174.    run if this is non-zero.  */
  175. static int error_count = 0;
  176.  
  177. /* This is the obstack which we use to allocate many strings.  */
  178.  
  179. static struct obstack obstack;
  180.  
  181. /* This is the obstack to build an environment variable to pass to
  182.    collect2 that describes all of the relevant switches of what to
  183.    pass the compiler in building the list of pointers to constructors
  184.    and destructors.  */
  185.  
  186. #if (!(defined(CROSSATARI) || defined(atarist)))
  187. static struct obstack collect_obstack;
  188. #endif
  189.  
  190. extern char *version_string;
  191.  
  192. static void set_spec ();
  193. static struct compiler *lookup_compiler ();
  194. static char *find_a_file ();
  195. static void add_prefix ();
  196. static char *skip_whitespace ();
  197. static void record_temp_file ();
  198. static char *handle_braces ();
  199. static char *save_string ();
  200. static char *concat ();
  201. static int do_spec ();
  202. static int do_spec_1 ();
  203. static char *find_file ();
  204. static int is_directory ();
  205. static void validate_switches ();
  206. static void validate_all_switches ();
  207. static void give_switch ();
  208. static void pfatal_with_name ();
  209. static void perror_with_name ();
  210. static void perror_exec ();
  211. static void fatal ();
  212. static void error ();
  213. void fancy_abort ();
  214. char *xmalloc ();
  215. char *xrealloc ();
  216.  
  217. /* Specs are strings containing lines, each of which (if not blank)
  218. is made up of a program name, and arguments separated by spaces.
  219. The program name must be exact and start from root, since no path
  220. is searched and it is unreliable to depend on the current working directory.
  221. Redirection of input or output is not supported; the subprograms must
  222. accept filenames saying what files to read and write.
  223.  
  224. In addition, the specs can contain %-sequences to substitute variable text
  225. or for conditional text.  Here is a table of all defined %-sequences.
  226. Note that spaces are not generated automatically around the results of
  227. expanding these sequences; therefore, you can concatenate them together
  228. or with constant text in a single argument.
  229.  
  230.  %%    substitute one % into the program name or argument.
  231.  %i     substitute the name of the input file being processed.
  232.  %b     substitute the basename of the input file being processed.
  233.     This is the substring up to (and not including) the last period
  234.     and not including the directory.
  235.  %g     substitute the temporary-file-name-base.  This is a string chosen
  236.     once per compilation.  Different temporary file names are made by
  237.     concatenation of constant strings on the end, as in `%g.s'.
  238.     %g also has the same effect of %d.
  239.  %u    like %g, but make the temporary file name unique.
  240.  %U    returns the last file name generated with %u.
  241.  %d    marks the argument containing or following the %d as a
  242.     temporary file name, so that that file will be deleted if CC exits
  243.     successfully.  Unlike %g, this contributes no text to the argument.
  244.  %w    marks the argument containing or following the %w as the
  245.     "output file" of this compilation.  This puts the argument
  246.     into the sequence of arguments that %o will substitute later.
  247.  %W{...}
  248.     like %{...} but mark last argument supplied within
  249.     as a file to be deleted on failure.
  250.  %o    substitutes the names of all the output files, with spaces
  251.     automatically placed around them.  You should write spaces
  252.     around the %o as well or the results are undefined.
  253.     %o is for use in the specs for running the linker.
  254.     Input files whose names have no recognized suffix are not compiled
  255.     at all, but they are included among the output files, so they will
  256.     be linked.
  257.  %p    substitutes the standard macro predefinitions for the
  258.     current target machine.  Use this when running cpp.
  259.  %P    like %p, but puts `__' before and after the name of each macro.
  260.     (Except macros that already have __.)
  261.     This is for ANSI C.
  262.  %I    Substitute a -iprefix option made from GCC_EXEC_PREFIX.
  263.  %s     current argument is the name of a library or startup file of some sort.
  264.         Search for that file in a standard list of directories
  265.     and substitute the full name found.
  266.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  267.         Use this when inconsistent options are detected.
  268.  %x{OPTION}    Accumulate an option for %X.
  269.  %X    Output the accumulated linker options specified by compilations.
  270.  %Y    Output the accumulated assembler options specified by compilations.
  271.  %v1    Substitute the major version number of GCC.
  272.     (For version 2.5.n, this is 2.)
  273.  %v2    Substitute the minor version number of GCC.
  274.     (For version 2.5.n, this is 5.)
  275.  %a     process ASM_SPEC as a spec.
  276.         This allows config.h to specify part of the spec for running as.
  277.  %A    process ASM_FINAL_SPEC as a spec.  A capital A is actually
  278.     used here.  This can be used to run a post-processor after the
  279.     assembler has done it's job.
  280.  %D    Dump out a -L option for each directory in startfile_prefix.
  281.  %l     process LINK_SPEC as a spec.
  282.  %L     process LIB_SPEC as a spec.
  283.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  284.  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
  285.  %c    process SIGNED_CHAR_SPEC as a spec.
  286.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  287.  %1    process CC1_SPEC as a spec.
  288.  %2    process CC1PLUS_SPEC as a spec.
  289.  %|    output "-" if the input for the current command is coming from a pipe.
  290.  %*    substitute the variable part of a matched option.  (See below.)
  291.     Note that each comma in the substituted string is replaced by
  292.     a single space.
  293.  %{S}   substitutes the -S switch, if that switch was given to CC.
  294.     If that switch was not specified, this substitutes nothing.
  295.     Here S is a metasyntactic variable.
  296.  %{S*}  substitutes all the switches specified to CC whose names start
  297.     with -S.  This is used for -o, -D, -I, etc; switches that take
  298.     arguments.  CC considers `-o foo' as being one switch whose
  299.     name starts with `o'.  %{o*} would substitute this text,
  300.     including the space; thus, two arguments would be generated.
  301.  %{S*:X} substitutes X if one or more switches whose names start with -S are
  302.     specified to CC.  Note that the tail part of the -S option
  303.     (i.e. the part matched by the `*') will be substituted for each
  304.     occurrence of %* within X.
  305.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  306.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  307.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  308.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  309.  %{.S:X} substitutes X, but only if processing a file with suffix S.
  310.  %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
  311.  %(Spec) processes a specification defined in a specs file as *Spec:
  312.  %[Spec] as above, but put __ around -D arguments
  313.  
  314. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  315. other nested % constructs or spaces, or even newlines.  They are
  316. processed as usual, as described above.
  317.  
  318. The character | is used to indicate that a command should be piped to
  319. the following command, but only if -pipe is specified.
  320.  
  321. Note that it is built into CC which switches take arguments and which
  322. do not.  You might think it would be useful to generalize this to
  323. allow each compiler's spec to say which switches take arguments.  But
  324. this cannot be done in a consistent fashion.  CC cannot even decide
  325. which input files have been specified without knowing which switches
  326. take arguments, and it must know which input files to compile in order
  327. to tell which compilers to run.
  328.  
  329. CC also knows implicitly that arguments starting in `-l' are to be
  330. treated as compiler output files, and passed to the linker in their
  331. proper position among the other output files.  */
  332.  
  333. /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1.  */
  334.  
  335. /* config.h can define ASM_SPEC to provide extra args to the assembler
  336.    or extra switch-translations.  */
  337. #ifndef ASM_SPEC
  338. #define ASM_SPEC ""
  339. #endif
  340.  
  341. /* config.h can define ASM_FINAL_SPEC to run a post processor after
  342.    the assembler has run.  */
  343. #ifndef ASM_FINAL_SPEC
  344. #define ASM_FINAL_SPEC ""
  345. #endif
  346.  
  347. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  348.    or extra switch-translations.  */
  349. #ifndef CPP_SPEC
  350. #define CPP_SPEC ""
  351. #endif
  352.  
  353. /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
  354.    or extra switch-translations.  */
  355. #ifndef CC1_SPEC
  356. #define CC1_SPEC ""
  357. #endif
  358.  
  359. /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
  360.    or extra switch-translations.  */
  361. #ifndef CC1PLUS_SPEC
  362. #define CC1PLUS_SPEC ""
  363. #endif
  364.  
  365. /* config.h can define LINK_SPEC to provide extra args to the linker
  366.    or extra switch-translations.  */
  367. #ifndef LINK_SPEC
  368. #define LINK_SPEC ""
  369. #endif
  370.  
  371. /* config.h can define LIB_SPEC to override the default libraries.  */
  372. #ifndef LIB_SPEC
  373. #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  374. #endif
  375.  
  376. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  377. #ifndef STARTFILE_SPEC
  378. #define STARTFILE_SPEC  \
  379.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  380. #endif
  381.  
  382. /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
  383.    Make the string nonempty to require spaces there.  */
  384. #ifndef SWITCHES_NEED_SPACES
  385. #define SWITCHES_NEED_SPACES ""
  386. #endif
  387.  
  388. /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
  389. #ifndef ENDFILE_SPEC
  390. #define ENDFILE_SPEC ""
  391. #endif
  392.  
  393. /* This spec is used for telling cpp whether char is signed or not.  */
  394. #ifndef SIGNED_CHAR_SPEC
  395. /* Use #if rather than ?:
  396.    because MIPS C compiler rejects like ?: in initializers.  */
  397. #if DEFAULT_SIGNED_CHAR
  398. #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
  399. #else
  400. #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
  401. #endif
  402. #endif
  403.  
  404. static char *cpp_spec = CPP_SPEC;
  405. static char *cpp_predefines = CPP_PREDEFINES;
  406. static char *cc1_spec = CC1_SPEC;
  407. static char *cc1plus_spec = CC1PLUS_SPEC;
  408. static char *signed_char_spec = SIGNED_CHAR_SPEC;
  409. static char *asm_spec = ASM_SPEC;
  410. static char *asm_final_spec = ASM_FINAL_SPEC;
  411. static char *link_spec = LINK_SPEC;
  412. static char *lib_spec = LIB_SPEC;
  413. static char *endfile_spec = ENDFILE_SPEC;
  414. static char *startfile_spec = STARTFILE_SPEC;
  415. static char *switches_need_spaces = SWITCHES_NEED_SPACES;
  416.  
  417. /* This defines which switch letters take arguments.  */
  418.  
  419. #ifndef SWITCH_TAKES_ARG
  420. #define SWITCH_TAKES_ARG(CHAR)      \
  421.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  422.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  423.    || (CHAR) == 'I' || (CHAR) == 'm' \
  424.    || (CHAR) == 'L' || (CHAR) == 'A')
  425. #endif
  426.  
  427. /* This defines which multi-letter switches take arguments.  */
  428.  
  429. #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR)        \
  430.  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")    \
  431.   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")    \
  432.   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
  433.   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
  434.   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore"))
  435.  
  436. #ifndef WORD_SWITCH_TAKES_ARG
  437. #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
  438. #endif
  439.  
  440. /* Record the mapping from file suffixes for compilation specs.  */
  441.  
  442. struct compiler
  443. {
  444.   char *suffix;            /* Use this compiler for input files
  445.                    whose names end in this suffix.  */
  446.  
  447.   char *spec[4];        /* To use this compiler, concatenate these
  448.                    specs and pass to do_spec.  */
  449. };
  450.  
  451. /* Pointer to a vector of `struct compiler' that gives the spec for
  452.    compiling a file, based on its suffix.
  453.    A file that does not end in any of these suffixes will be passed
  454.    unchanged to the loader and nothing else will be done to it.
  455.  
  456.    An entry containing two 0s is used to terminate the vector.
  457.  
  458.    If multiple entries match a file, the last matching one is used.  */
  459.  
  460. static struct compiler *compilers;
  461.  
  462. /* Number of entries in `compilers', not counting the null terminator.  */
  463.  
  464. static int n_compilers;
  465.  
  466. /* The default list of file name suffixes and their compilation specs.  */
  467.  
  468. static struct compiler default_compilers[] =
  469. {
  470.   {".c", "@c"},
  471.   {"@c",
  472.    "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  473.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  474.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  475.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  476.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  477.     %{!undef:%{!ansi:%p} %P} %{trigraphs} \
  478.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  479.         %{traditional-cpp:-traditional}\
  480.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  481.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  482.    "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
  483.            %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
  484.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  485.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
  486.            %{aux-info*}\
  487.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  488.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  489.               %{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  490.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  491.                       %{!pipe:%g.s} %A\n }}}}"},
  492.   {"-",
  493.    "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  494.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  495.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  496.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  497.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  498.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  499.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  500.         %{traditional-cpp:-traditional}\
  501.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  502.         %i %W{o*}}\
  503.     %{!E:%e-E required when input is from standard input}"},
  504.   {".m", "@objective-c"},
  505.   {"@objective-c",
  506.    "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  507.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  508.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  509.         -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  510.      %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  511.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  512.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  513.         %{traditional-cpp:-traditional}\
  514.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  515.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  516.    "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
  517.            %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
  518.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  519.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
  520.                -lang-objc %{gen-decls} \
  521.            %{aux-info*}\
  522.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  523.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  524.               %{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  525.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  526.                       %{!pipe:%g.s} %A\n }}}}"},
  527.   {".h", "@c-header"},
  528.   {"@c-header",
  529.    "%{!E:%eCompilation of header file requested} \
  530.     cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  531.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  532.      %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
  533.         -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
  534.      %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  535.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  536.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  537.         %{traditional-cpp:-traditional}\
  538.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  539.         %i %W{o*}"},
  540.   {".cc", "@c++"},
  541.   {".cxx", "@c++"},
  542.   {".C", "@c++"},
  543.   {"@c++",
  544.    "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  545.     %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
  546.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
  547.     -undef -D__GNUC__=%v1 -D__GNUG__=%v1 -D__cplusplus -D__GNUC_MINOR__=%v2\
  548.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
  549.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  550.         %{traditional-cpp:-traditional} %{trigraphs}\
  551.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  552.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  553.    "%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
  554.            %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
  555.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  556.            %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
  557.            %{aux-info*}\
  558.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  559.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  560.               %{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  561.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  562.                       %{!pipe:%g.s} %A\n }}}}"},
  563.   {".i", "@cpp-output"},
  564.   {"@cpp-output",
  565.    "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
  566.     %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  567.     %{v:-version} %{pg:-p} %{p} %{f*}\
  568.     %{aux-info*}\
  569.     %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  570.     %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  571.     %{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  572.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %{!pipe:%g.s} %A\n }"},
  573.   {".ii", "@c++-cpp-output"},
  574.   {"@c++-cpp-output",
  575.    "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
  576.         %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  577.         %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
  578.         %{aux-info*}\
  579.         %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  580.         %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  581.        %{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  582.            %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  583.            %{!pipe:%g.s} %A\n }"},
  584.   {".s", "@assembler"},
  585.   {"@assembler",
  586.    "%{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  587.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %i %A\n }"},
  588.   {".S", "@assembler-with-cpp"},
  589.   {".ss", "@assembler-with-cpp"},
  590.   {".cpp", "@assembler-with-cpp"},
  591.   {".spp", "@assembler-with-cpp"},
  592.   {"@assembler-with-cpp",
  593.    "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  594.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  595.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
  596.         -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
  597.         %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
  598.         %{traditional-cpp:-traditional}\
  599.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  600.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  601.    "%{!M:%{!MM:%{!E:%{!S:as %{v} %{R} %{j} %{J} %{h} %{d2} %a %Y\
  602.                     %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  603.             %{!pipe:%g.s} %A\n }}}}"},
  604.   {".ads", "@ada"},
  605.   {".adb", "@ada"},
  606.   {".ada", "@ada"},
  607.   {"@ada",
  608.    "gnat1 %{gnat*} %{k8:-gnatk8} %{w:-gnatws}\
  609.        -dumpbase %b.ada\
  610.        %{g*} %{O*} %{p} %{pg:-p} %{f*} %{d*}\
  611.        %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  612.        %i %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} | \n",
  613.    "%{!S:%{!gnatc:%{!gnats:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  614.         %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  615.         %{!pipe:%g.s} %A\n}}} "},
  616.   /* Mark end of table */
  617.   {0, 0}
  618. };
  619.  
  620. /* Number of elements in default_compilers, not counting the terminator.  */
  621.  
  622. static int n_default_compilers
  623.   = (sizeof default_compilers / sizeof (struct compiler)) - 1;
  624.  
  625. /* Here is the spec for running the linker, after compiling all files.  */
  626.  
  627. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  628. static char *link_command_spec = 
  629.  "%{!fsyntax-only: \
  630.     %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{v} %{G} %{o*} \
  631.       %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{t} %{u*} %{x} %{z}\
  632.       %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
  633.       %{L*} %{T*} %o %{!nostdlib:%L}\n }}}}}}";
  634.  
  635. #else
  636. /* -u* was put back because both BSD and SysV seem to support it.  */
  637. /* %{static:} simply prevents an error message if the target machine
  638.    doesn't handle -static.  */
  639. /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
  640.    scripts which exist in user specified directories, or in standard
  641.    directories.  */
  642. #ifdef LINK_LIBGCC_SPECIAL_1
  643. /* Have gcc do the search for libgcc.a, but generate -L options as usual.  */
  644. static char *link_command_spec = "\
  645. %{!fsyntax-only: \
  646.  %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  647.             %{r} %{s} %{t} %{u*} %{x} %{z}\
  648.             %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
  649.             %{L*} %D %{T*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}}";
  650. #else
  651. #ifdef LINK_LIBGCC_SPECIAL
  652. /* Have gcc do the search for libgcc.a, and don't generate -L options.  */
  653. static char *link_command_spec = "\
  654. %{!fsyntax-only: \
  655.  %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  656.             %{r} %{s} %{t} %{u*} %{x} %{z}\
  657.             %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
  658.             %{L*} %{T*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}}";
  659. #else
  660. /* Use -L and have the linker do the search for -lgcc.  */
  661. static char *link_command_spec = "\
  662. %{!fsyntax-only: \
  663.  %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  664.             %{r} %{s} %{t} %{u*} %{x} %{z}\
  665.             %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
  666.             %{L*} %D %{T*} %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}}";
  667. #endif
  668. #endif
  669. #endif
  670.  
  671. /* A vector of options to give to the linker.
  672.    These options are accumulated by -Xlinker and -Wl,
  673.    and substituted into the linker command with %X.  */
  674. static int n_linker_options;
  675. static char **linker_options;
  676.  
  677. /* A vector of options to give to the assembler.
  678.    These options are accumulated by -Wa,
  679.    and substituted into the assembler command with %X.  */
  680. static int n_assembler_options;
  681. static char **assembler_options;
  682.  
  683. /* Define how to map long options into short ones.  */
  684.  
  685. /* This structure describes one mapping.  */
  686. struct option_map
  687. {
  688.   /* The long option's name.  */
  689.   char *name;
  690.   /* The equivalent short option.  */
  691.   char *equivalent;
  692.   /* Argument info.  A string of flag chars; NULL equals no options.
  693.      a => argument required.
  694.      o => argument optional.
  695.      j => join argument to equivalent, making one word.
  696.      * => allow other text after NAME as an argument.  */
  697.   char *arg_info;
  698. };
  699.  
  700. /* This is the table of mappings.  Mappings are tried sequentially
  701.    for each option encountered; the first one that matches, wins.  */
  702.  
  703. struct option_map option_map[] =
  704.  {
  705.    {"--profile-blocks", "-a", 0},
  706.    {"--target", "-b", "a"},
  707.    {"--compile", "-c", 0},
  708.    {"--dump", "-d", "a"},
  709.    {"--entry", "-e", 0},
  710.    {"--debug", "-g", "oj"},
  711.    {"--include", "-include", "a"},
  712.    {"--imacros", "-imacros", "a"},
  713.    {"--include-prefix", "-iprefix", "a"},
  714.    {"--include-directory-after", "-idirafter", "a"},
  715.    {"--include-with-prefix", "-iwithprefix", "a"},
  716.    {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
  717.    {"--include-with-prefix-after", "-iwithprefix", "a"},
  718.    {"--machine-", "-m", "*j"},
  719.    {"--machine", "-m", "aj"},
  720.    {"--no-standard-includes", "-nostdinc", 0},
  721.    {"--no-standard-libraries", "-nostdlib", 0},
  722.    {"--no-precompiled-includes", "-noprecomp", 0},
  723.    {"--output", "-o", "a"},
  724.    {"--profile", "-p", 0},
  725.    {"--quiet", "-q", 0},
  726.    {"--silent", "-q", 0},
  727.    {"--force-link", "-u", "a"},
  728.    {"--verbose", "-v", 0},
  729.    {"--version", "-dumpversion", 0},
  730.    {"--no-warnings", "-w", 0},
  731.    {"--language", "-x", "a"},
  732.  
  733.    {"--assert", "-A", "a"},
  734.    {"--prefix", "-B", "a"},
  735.    {"--comments", "-C", 0},
  736.    {"--define-macro", "-D", "a"},
  737.    {"--preprocess", "-E", 0},
  738.    {"--trace-includes", "-H", 0},
  739.    {"--include-directory", "-I", "a"},
  740.    {"--include-barrier", "-I-", 0},
  741.    {"--library-directory", "-L", "a"},
  742.    {"--dependencies", "-M", 0},
  743.    {"--user-dependencies", "-MM", 0},
  744.    {"--write-dependencies", "-MD", 0},
  745.    {"--write-user-dependencies", "-MMD", 0},
  746.    {"--optimize", "-O", "oj"},
  747.    {"--no-line-commands", "-P", 0},
  748.    {"--assemble", "-S", 0},
  749.    {"--undefine-macro", "-U", "a"},
  750.    {"--use-version", "-V", "a"},
  751.    {"--for-assembler", "-Wa", "a"},
  752.    {"--extra-warnings", "-W", 0},
  753.    {"--all-warnings", "-Wall", 0},
  754.    {"--warn-", "-W", "*j"},
  755.    {"--for-linker", "-Xlinker", "a"},
  756.  
  757.    {"--ansi", "-ansi", 0},
  758.    {"--traditional", "-traditional", 0},
  759.    {"--traditional-cpp", "-traditional-cpp", 0},
  760.    {"--trigraphs", "-trigraphs", 0},
  761.    {"--pipe", "-pipe", 0},
  762.    {"--dumpbase", "-dumpbase", "a"},
  763.    {"--pedantic", "-pedantic", 0},
  764.    {"--pedantic-errors", "-pedantic-errors", 0},
  765.    {"--save-temps", "-save-temps", 0},
  766.    {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
  767.    {"--static", "-static", 0},
  768.    {"--shared", "-shared", 0},
  769.    {"--symbolic", "-symbolic", 0},
  770.    {"--", "-f", "*j"}
  771.  };
  772.  
  773. /* Translate the options described by *ARGCP and *ARGVP.
  774.    Make a new vector and store it back in *ARGVP,
  775.    and store its length in *ARGVC.  */
  776.  
  777. static void
  778. translate_options (argcp, argvp)
  779.      int *argcp;
  780.      char ***argvp;
  781. {
  782.   int i, j;
  783.   int argc = *argcp;
  784.   char **argv = *argvp;
  785.   char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
  786.   int newindex = 0;
  787.  
  788.   i = 0;
  789.   newv[newindex++] = argv[i++];
  790.  
  791.   while (i < argc)
  792.     {
  793.       /* Translate -- options.  */
  794.       if (argv[i][0] == '-' && argv[i][1] == '-')
  795.     {
  796.       /* Find a mapping that applies to this option.  */
  797.       for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
  798.         {
  799.           int optlen = strlen (option_map[j].name);
  800.           int complen = strlen (argv[i]);
  801.           char *arginfo = option_map[j].arg_info;
  802.  
  803.           if (arginfo == 0)
  804.         arginfo = "";
  805.           if (complen > optlen)
  806.         complen = optlen;
  807.           if (!strncmp (argv[i], option_map[j].name, complen))
  808.         {
  809.           int extra = strlen (argv[i]) > optlen;
  810.           char *arg = 0;
  811.  
  812.           if (extra)
  813.             {
  814.               /* If the option has an argument, accept that.  */
  815.               if (argv[i][optlen] == '=')
  816.             arg = argv[i] + optlen + 1;
  817.               /* If this mapping allows extra text at end of name,
  818.              accept that as "argument".  */
  819.               else if (index (arginfo, '*') != 0)
  820.             arg = argv[i] + optlen;
  821.               /* Otherwise, extra text at end means mismatch.
  822.              Try other mappings.  */
  823.               else
  824.             continue;
  825.             }
  826.           else if (index (arginfo, '*') != 0)
  827.             error ("Incomplete `%s' option", option_map[j].name);
  828.  
  829.           /* Handle arguments.  */
  830.           if (index (arginfo, 'o') != 0)
  831.             {
  832.               if (arg == 0)
  833.             {
  834.               if (i + 1 == argc)
  835.                 error ("Missing argument to `%s' option",
  836.                    option_map[j].name);
  837.               arg = argv[++i];
  838.             }
  839.             }
  840.           else if (index (arginfo, '*') != 0)
  841.             ;
  842.           else if (index (arginfo, 'a') == 0)
  843.             {
  844.               if (arg != 0)
  845.             error ("Extraneous argument to `%s' option",
  846.                    option_map[j].name);
  847.               arg = 0;
  848.             }
  849.  
  850.           /* Store the translation as one argv elt or as two.  */
  851.           if (arg != 0 && index (arginfo, 'j') != 0)
  852.             newv[newindex++] = concat (option_map[j].equivalent,
  853.                            arg, "");
  854.           else if (arg != 0)
  855.             {
  856.               newv[newindex++] = option_map[j].equivalent;
  857.               newv[newindex++] = arg;
  858.             }
  859.           else
  860.             newv[newindex++] = option_map[j].equivalent;
  861.  
  862.           break;
  863.         }
  864.         }
  865.       i++;
  866.     }
  867.       /* Handle old-fashioned options--just copy them through,
  868.      with their arguments.  */
  869.       else if (argv[i][0] == '-')
  870.     {
  871.       char *p = argv[i] + 1;
  872.       int c = *p;
  873.       int nskip = 1;
  874.  
  875.       if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
  876.         nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
  877.       else if (WORD_SWITCH_TAKES_ARG (p))
  878.         nskip += WORD_SWITCH_TAKES_ARG (p);
  879.  
  880.       while (nskip > 0)
  881.         {
  882.           newv[newindex++] = argv[i++];
  883.           nskip--;
  884.         }
  885.     }
  886.       else
  887.     /* Ordinary operands, or +e options.  */
  888.     newv[newindex++] = argv[i++];
  889.     }
  890.  
  891.   newv[newindex] = 0;
  892.  
  893.   *argvp = newv;
  894.   *argcp = newindex;
  895. }
  896.  
  897. #if (!(defined(atarist) || defined(CROSSATARI) || defined(atariminix)))
  898. /* Read compilation specs from a file named FILENAME,
  899.    replacing the default ones.
  900.  
  901.    A suffix which starts with `*' is a definition for
  902.    one of the machine-specific sub-specs.  The "suffix" should be
  903.    *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
  904.    The corresponding spec is stored in asm_spec, etc.,
  905.    rather than in the `compilers' vector.
  906.  
  907.    Anything invalid in the file is a fatal error.  */
  908.  
  909. static void
  910. read_specs (filename)
  911.      char *filename;
  912. {
  913.   int desc;
  914.   struct stat statbuf;
  915.   char *buffer;
  916.   register char *p;
  917.  
  918.   if (verbose_flag)
  919.     fprintf (stderr, "Reading specs from %s\n", filename);
  920.  
  921.   /* Open and stat the file.  */
  922.   desc = open (filename, 0, 0);
  923.   if (desc < 0)
  924.     pfatal_with_name (filename);
  925.   if (stat (filename, &statbuf) < 0)
  926.     pfatal_with_name (filename);
  927.  
  928.   /* Read contents of file into BUFFER.  */
  929.   buffer = xmalloc ((unsigned) statbuf.st_size + 1);
  930.   read (desc, buffer, (unsigned) statbuf.st_size);
  931.   buffer[statbuf.st_size] = 0;
  932.   close (desc);
  933.  
  934.   /* Scan BUFFER for specs, putting them in the vector.  */
  935.   p = buffer;
  936.   while (1)
  937.     {
  938.       char *suffix;
  939.       char *spec;
  940.       char *in, *out, *p1, *p2;
  941.  
  942.       /* Advance P in BUFFER to the next nonblank nocomment line.  */
  943.       p = skip_whitespace (p);
  944.       if (*p == 0)
  945.     break;
  946.  
  947.       /* Find the colon that should end the suffix.  */
  948.       p1 = p;
  949.       while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
  950.       /* The colon shouldn't be missing.  */
  951.       if (*p1 != ':')
  952.     fatal ("specs file malformed after %d characters", p1 - buffer);
  953.       /* Skip back over trailing whitespace.  */
  954.       p2 = p1;
  955.       while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
  956.       /* Copy the suffix to a string.  */
  957.       suffix = save_string (p, p2 - p);
  958.       /* Find the next line.  */
  959.       p = skip_whitespace (p1 + 1);
  960.       if (p[1] == 0)
  961.     fatal ("specs file malformed after %d characters", p - buffer);
  962.       p1 = p;
  963.       /* Find next blank line.  */
  964.       while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
  965.       /* Specs end at the blank line and do not include the newline.  */
  966.       spec = save_string (p, p1 - p);
  967.       p = p1;
  968.  
  969.       /* Delete backslash-newline sequences from the spec.  */
  970.       in = spec;
  971.       out = spec;
  972.       while (*in != 0)
  973.     {
  974.       if (in[0] == '\\' && in[1] == '\n')
  975.         in += 2;
  976.       else if (in[0] == '#')
  977.         {
  978.           while (*in && *in != '\n') in++;
  979.         }
  980.       else
  981.         *out++ = *in++;
  982.     }
  983.       *out = 0;
  984.  
  985.       if (suffix[0] == '*')
  986.     {
  987.       if (! strcmp (suffix, "*link_command"))
  988.         link_command_spec = spec;
  989.       else
  990.         set_spec (suffix + 1, spec);
  991.     }
  992.       else
  993.     {
  994.       /* Add this pair to the vector.  */
  995.       compilers
  996.         = ((struct compiler *)
  997.            xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
  998.       compilers[n_compilers].suffix = suffix;
  999.       bzero (compilers[n_compilers].spec,
  1000.          sizeof compilers[n_compilers].spec);
  1001.       compilers[n_compilers].spec[0] = spec;
  1002.       n_compilers++;
  1003.       bzero (&compilers[n_compilers], sizeof compilers[n_compilers]);
  1004.     }
  1005.  
  1006.       if (*suffix == 0)
  1007.     link_command_spec = spec;
  1008.     }
  1009.  
  1010.   if (link_command_spec == 0)
  1011.     fatal ("spec file has no spec for linking");
  1012. }
  1013.  
  1014. static char *
  1015. skip_whitespace (p)
  1016.      char *p;
  1017. {
  1018.   while (1)
  1019.     {
  1020.       /* A fully-blank line is a delimiter in the SPEC file and shouldn't
  1021.      be considered whitespace.  */
  1022.       if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
  1023.     return p + 1;
  1024.       else if (*p == '\n' || *p == ' ' || *p == '\t')
  1025.     p++;
  1026.       else if (*p == '#')
  1027.     {
  1028.       while (*p != '\n') p++;
  1029.       p++;
  1030.     }
  1031.       else
  1032.     break;
  1033.     }
  1034.  
  1035.   return p;
  1036. }
  1037. #endif /* ataris */
  1038.  
  1039. /* Structure to keep track of the specs that have been defined so far.  These
  1040.    are accessed using %(specname) or %[specname] in a compiler or link spec. */
  1041.  
  1042. struct spec_list
  1043. {
  1044.   char *name;                 /* Name of the spec. */
  1045.   char *spec;                 /* The spec itself. */
  1046.   struct spec_list *next;     /* Next spec in linked list. */
  1047. };
  1048.  
  1049. /* List of specs that have been defined so far. */
  1050.  
  1051. static struct spec_list *specs = (struct spec_list *) 0;
  1052.  
  1053. /* Change the value of spec NAME to SPEC.  If SPEC is empty, then the spec is
  1054.    removed; If the spec starts with a + then SPEC is added to the end of the
  1055.    current spec. */
  1056.  
  1057. static void
  1058. set_spec (name, spec)
  1059.      char *name;
  1060.      char *spec;
  1061. {
  1062.   struct spec_list *sl;
  1063.   char *old_spec;
  1064.  
  1065.   /* See if the spec already exists */
  1066.   for (sl = specs; sl; sl = sl->next)
  1067.     if (strcmp (sl->name, name) == 0)
  1068.       break;
  1069.  
  1070.   if (!sl)
  1071.     {
  1072.       /* Not found - make it */
  1073.       sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
  1074.       sl->name = save_string (name, strlen (name));
  1075.       sl->spec = save_string ("", 0);
  1076.       sl->next = specs;
  1077.       specs = sl;
  1078.     }
  1079.  
  1080.   old_spec = sl->spec;
  1081.   if (name && spec[0] == '+' && isspace (spec[1]))
  1082.     sl->spec = concat (old_spec, spec + 1, "");
  1083.   else
  1084.     sl->spec = save_string (spec, strlen (spec));
  1085.  
  1086.   if (! strcmp (name, "asm"))
  1087.     asm_spec = sl->spec;
  1088.   else if (! strcmp (name, "asm_final"))
  1089.     asm_final_spec = sl->spec;
  1090.   else if (! strcmp (name, "cc1"))
  1091.     cc1_spec = sl->spec;
  1092.   else if (! strcmp (name, "cc1plus"))
  1093.     cc1plus_spec = sl->spec;
  1094.   else if (! strcmp (name, "cpp"))
  1095.     cpp_spec = sl->spec;
  1096.   else if (! strcmp (name, "endfile"))
  1097.     endfile_spec = sl->spec;
  1098.   else if (! strcmp (name, "lib"))
  1099.     lib_spec = sl->spec;
  1100.   else if (! strcmp (name, "link"))
  1101.     link_spec = sl->spec;
  1102.   else if (! strcmp (name, "predefines"))
  1103.     cpp_predefines = sl->spec;
  1104.   else if (! strcmp (name, "signed_char"))
  1105.     signed_char_spec = sl->spec;
  1106.   else if (! strcmp (name, "startfile"))
  1107.     startfile_spec = sl->spec;
  1108.   else if (! strcmp (name, "switches_need_spaces"))
  1109.     switches_need_spaces = sl->spec;
  1110.   else if (! strcmp (name, "cross_compile"))
  1111.     cross_compile = atoi (sl->spec);
  1112.   /* Free the old spec */
  1113.   if (old_spec)
  1114.     free (old_spec);
  1115. }
  1116.  
  1117. /* Accumulate a command (program name and args), and run it.  */
  1118.  
  1119. /* Vector of pointers to arguments in the current line of specifications.  */
  1120.  
  1121. static char **argbuf;
  1122.  
  1123. /* Number of elements allocated in argbuf.  */
  1124.  
  1125. static int argbuf_length;
  1126.  
  1127. /* Number of elements in argbuf currently in use (containing args).  */
  1128.  
  1129. static int argbuf_index;
  1130.  
  1131. /* This is the list of suffixes and codes (%g/%u/%U) and the associated
  1132.    temp file.  Used only if MKTEMP_EACH_FILE.  */
  1133.  
  1134. static struct temp_name {
  1135.   char *suffix;        /* suffix associated with the code.  */
  1136.   int length;        /* strlen (suffix).  */
  1137.   int unique;        /* Indicates whether %g or %u/%U was used.  */
  1138.   char *filename;    /* associated filename.  */
  1139.   int filename_length;    /* strlen (filename).  */
  1140.   struct temp_name *next;
  1141. } *temp_names;
  1142.  
  1143. /* Number of commands executed so far.  */
  1144.  
  1145. static int execution_count;
  1146.  
  1147. /* Number of commands that exited with a signal.  */
  1148.  
  1149. static int signal_count;
  1150.  
  1151. /* Name with which this program was invoked.  */
  1152.  
  1153. static char *programname;
  1154.  
  1155. #ifdef atarist
  1156. /* Flag indicating, that stderr should be redirected for the child
  1157.    processes */
  1158.  
  1159. unsigned char zflag;
  1160. #endif
  1161.  
  1162. /* Structures to keep track of prefixes to try when looking for files. */
  1163.  
  1164. struct prefix_list
  1165. {
  1166.   char *prefix;               /* String to prepend to the path. */
  1167.   struct prefix_list *next;   /* Next in linked list. */
  1168.   int require_machine_suffix; /* Don't use without machine_suffix.  */
  1169.   /* 2 means try both machine_suffix and just_machine_suffix.  */
  1170.   int *used_flag_ptr;          /* 1 if a file was found with this prefix.  */
  1171. };
  1172.  
  1173. struct path_prefix
  1174. {
  1175.   struct prefix_list *plist;  /* List of prefixes to try */
  1176.   int max_len;                /* Max length of a prefix in PLIST */
  1177.   char *name;                 /* Name of this list (used in config stuff) */
  1178. };
  1179.  
  1180. /* List of prefixes to try when looking for executables. */
  1181.  
  1182. static struct path_prefix exec_prefix = { 0, 0, "exec" };
  1183.  
  1184. /* List of prefixes to try when looking for startup (crt0) files. */
  1185.  
  1186. static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
  1187.  
  1188. /* Suffix to attach to directories searched for commands.
  1189.    This looks like `MACHINE/VERSION/'.  */
  1190.  
  1191. static char *machine_suffix = 0;
  1192.  
  1193. /* Suffix to attach to directories searched for commands.
  1194.    This is just `MACHINE/'.  */
  1195.  
  1196. static char *just_machine_suffix = 0;
  1197.  
  1198. /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
  1199.  
  1200. static char *gcc_exec_prefix;
  1201.  
  1202. /* Default prefixes to attach to command names.  */
  1203.  
  1204. #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
  1205. #undef MD_EXEC_PREFIX
  1206. #undef MD_STARTFILE_PREFIX
  1207. #undef MD_STARTFILE_PREFIX_1
  1208. #endif
  1209.  
  1210. #ifndef STANDARD_EXEC_PREFIX
  1211. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
  1212. #endif /* !defined STANDARD_EXEC_PREFIX */
  1213.  
  1214. static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  1215. static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
  1216. #ifdef MD_EXEC_PREFIX
  1217. static char *md_exec_prefix = MD_EXEC_PREFIX;
  1218. #endif
  1219.  
  1220. #ifndef STANDARD_STARTFILE_PREFIX
  1221. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  1222. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  1223.  
  1224. #ifdef MD_STARTFILE_PREFIX
  1225. static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
  1226. #endif
  1227. #ifdef MD_STARTFILE_PREFIX_1
  1228. static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
  1229. #endif
  1230. static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  1231. static char *standard_startfile_prefix_1 = "/lib/";
  1232. static char *standard_startfile_prefix_2 = "/usr/lib/";
  1233.  
  1234. #ifndef TOOLDIR_BASE_PREFIX
  1235. #define TOOLDIR_BASE_PREFIX "/usr/local/"
  1236. #endif
  1237. static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
  1238. static char *tooldir_prefix;
  1239.  
  1240. /* Clear out the vector of arguments (after a command is executed).  */
  1241.  
  1242. static void
  1243. clear_args ()
  1244. {
  1245.   argbuf_index = 0;
  1246. }
  1247.  
  1248. /* Add one argument to the vector at the end.
  1249.    This is done when a space is seen or at the end of the line.
  1250.    If DELETE_ALWAYS is nonzero, the arg is a filename
  1251.     and the file should be deleted eventually.
  1252.    If DELETE_FAILURE is nonzero, the arg is a filename
  1253.     and the file should be deleted if this compilation fails.  */
  1254.  
  1255. static void
  1256. store_arg (arg, delete_always, delete_failure)
  1257.      char *arg;
  1258.      int delete_always, delete_failure;
  1259. {
  1260.   if (argbuf_index + 1 == argbuf_length)
  1261.     {
  1262.       argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  1263.     }
  1264.  
  1265.   argbuf[argbuf_index++] = arg;
  1266.   argbuf[argbuf_index] = 0;
  1267.  
  1268.   if (delete_always || delete_failure)
  1269.     record_temp_file (arg, delete_always, delete_failure);
  1270. }
  1271.  
  1272. /* Record the names of temporary files we tell compilers to write,
  1273.    and delete them at the end of the run.  */
  1274.  
  1275. /* This is the common prefix we use to make temp file names.
  1276.    It is chosen once for each run of this program.
  1277.    It is substituted into a spec by %g.
  1278.    Thus, all temp file names contain this prefix.
  1279.    In practice, all temp file names start with this prefix.
  1280.  
  1281.    This prefix comes from the envvar TMPDIR if it is defined;
  1282.    otherwise, from the P_tmpdir macro if that is defined;
  1283.    otherwise, in /usr/tmp or /tmp.  */
  1284.  
  1285. static char *temp_filename;
  1286.  
  1287. /* Length of the prefix.  */
  1288.  
  1289. static int temp_filename_length;
  1290.  
  1291. /* Define the list of temporary files to delete.  */
  1292.  
  1293. struct temp_file
  1294. {
  1295.   char *name;
  1296.   struct temp_file *next;
  1297. };
  1298.  
  1299. /* Queue of files to delete on success or failure of compilation.  */
  1300. static struct temp_file *always_delete_queue;
  1301. /* Queue of files to delete on failure of compilation.  */
  1302. static struct temp_file *failure_delete_queue;
  1303.  
  1304. /* Record FILENAME as a file to be deleted automatically.
  1305.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  1306.    otherwise delete it in any case.
  1307.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  1308.    otherwise delete it in any case.  */
  1309.  
  1310. static void
  1311. record_temp_file (filename, always_delete, fail_delete)
  1312.      char *filename;
  1313.      int always_delete;
  1314.      int fail_delete;
  1315. {
  1316.   register char *name;
  1317.   name = xmalloc (strlen (filename) + 1);
  1318.   strcpy (name, filename);
  1319.  
  1320.   if (always_delete)
  1321.     {
  1322.       register struct temp_file *temp;
  1323.       for (temp = always_delete_queue; temp; temp = temp->next)
  1324.     if (! strcmp (name, temp->name))
  1325.       goto already1;
  1326.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1327.       temp->next = always_delete_queue;
  1328.       temp->name = name;
  1329.       always_delete_queue = temp;
  1330.     already1:;
  1331.     }
  1332.  
  1333.   if (fail_delete)
  1334.     {
  1335.       register struct temp_file *temp;
  1336.       for (temp = failure_delete_queue; temp; temp = temp->next)
  1337.     if (! strcmp (name, temp->name))
  1338.       goto already2;
  1339.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1340.       temp->next = failure_delete_queue;
  1341.       temp->name = name;
  1342.       failure_delete_queue = temp;
  1343.     already2:;
  1344.     }
  1345. }
  1346.  
  1347. /* Delete all the temporary files whose names we previously recorded.  */
  1348.  
  1349. static void
  1350. delete_temp_files ()
  1351. {
  1352.   register struct temp_file *temp;
  1353.  
  1354.   for (temp = always_delete_queue; temp; temp = temp->next)
  1355.     {
  1356. #ifdef DEBUG
  1357.       int i;
  1358.       printf ("Delete %s? (y or n) ", temp->name);
  1359.       fflush (stdout);
  1360.       i = getchar ();
  1361.       if (i != '\n')
  1362.     while (getchar () != '\n') ;
  1363.       if (i == 'y' || i == 'Y')
  1364. #endif /* DEBUG */
  1365.     {
  1366.       struct stat st;
  1367.       if (stat (temp->name, &st) >= 0)
  1368.         {
  1369.           /* Delete only ordinary files.  */
  1370.           if (S_ISREG (st.st_mode))
  1371.         if (unlink (temp->name) < 0)
  1372.           if (verbose_flag)
  1373.             perror_with_name (temp->name);
  1374.         }
  1375.     }
  1376.     }
  1377.  
  1378.   always_delete_queue = 0;
  1379. }
  1380.  
  1381. /* Delete all the files to be deleted on error.  */
  1382.  
  1383. static void
  1384. delete_failure_queue ()
  1385. {
  1386.   register struct temp_file *temp;
  1387.  
  1388.   for (temp = failure_delete_queue; temp; temp = temp->next)
  1389.     {
  1390. #ifdef DEBUG
  1391.       int i;
  1392.       printf ("Delete %s? (y or n) ", temp->name);
  1393.       fflush (stdout);
  1394.       i = getchar ();
  1395.       if (i != '\n')
  1396.     while (getchar () != '\n') ;
  1397.       if (i == 'y' || i == 'Y')
  1398. #endif /* DEBUG */
  1399.     {
  1400.       if (unlink (temp->name) < 0)
  1401.         if (verbose_flag)
  1402.           perror_with_name (temp->name);
  1403.     }
  1404.     }
  1405. }
  1406.  
  1407. static void
  1408. clear_failure_queue ()
  1409. {
  1410.   failure_delete_queue = 0;
  1411. }
  1412.  
  1413. /* Compute a string to use as the base of all temporary file names.
  1414.    It is substituted for %g.  */
  1415.  
  1416. static char *
  1417. choose_temp_base_try (try, base)
  1418. char *try;
  1419. char *base;
  1420. {
  1421.   char *rv;
  1422.   if (base)
  1423.     rv = base;
  1424.   else if (try == (char *)0)
  1425.     rv = 0;
  1426.   else if (access (try, R_OK | W_OK) != 0)
  1427.     rv = 0;
  1428.   else
  1429.     rv = try;
  1430.   return rv;
  1431. }
  1432.  
  1433. static void
  1434. choose_temp_base ()
  1435. {
  1436.   char *base = 0;
  1437.   int len;
  1438.  
  1439.   base = choose_temp_base_try (getenv ("TMPDIR"), base);
  1440.   base = choose_temp_base_try (getenv ("TMP"), base);
  1441.   base = choose_temp_base_try (getenv ("TEMP"), base);
  1442.  
  1443. #ifdef P_tmpdir
  1444.   base = choose_temp_base_try (P_tmpdir, base);
  1445. #endif
  1446.  
  1447.   base = choose_temp_base_try ("/usr/tmp", base);
  1448.   base = choose_temp_base_try ("/tmp", base);
  1449.  
  1450.   /* If all else fails, use the current directory! */  
  1451.   if (base == (char *)0)
  1452.     base = "./";
  1453.  
  1454. #ifdef atarist
  1455.   {
  1456.       char *newbase = alloca(FILENAME_MAX);
  1457.       dos2unx(base,newbase);
  1458.       base = newbase;
  1459.   }
  1460. #endif
  1461.   len = strlen (base);
  1462.   temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
  1463.   strcpy (temp_filename, base);
  1464.   if (len > 0 && (temp_filename[len-1] != '/' 
  1465. #ifdef atarist
  1466.           || temp_filename[len-1] != '\\'
  1467. #endif
  1468.            ))
  1469.     temp_filename[len++] = '/';
  1470.   strcpy (temp_filename + len, "ccXXXXXX");
  1471.  
  1472.   mktemp (temp_filename);
  1473.   temp_filename_length = strlen (temp_filename);
  1474.   if (temp_filename_length == 0)
  1475.     abort ();
  1476. }
  1477.  
  1478.  
  1479. /* Routine to add variables to the environment.  We do this to pass
  1480.    the pathname of the gcc driver, and the directories search to the
  1481.    collect2 program, which is being run as ld.  This way, we can be
  1482.    sure of executing the right compiler when collect2 wants to build
  1483.    constructors and destructors.  Since the environment variables we
  1484.    use come from an obstack, we don't have to worry about allocating
  1485.    space for them.  */
  1486.  
  1487. #if (!(defined(CROSSATARI) || defined(atarist)))
  1488. #ifndef HAVE_PUTENV
  1489.  
  1490. void
  1491. putenv (str)
  1492.      char *str;
  1493. {
  1494. #ifndef VMS            /* nor about VMS */
  1495.  
  1496.   extern char **environ;
  1497.   char **old_environ = environ;
  1498.   char **envp;
  1499.   int num_envs = 0;
  1500.   int name_len = 1;
  1501.   int str_len = strlen (str);
  1502.   char *p = str;
  1503.   int ch;
  1504.  
  1505.   while ((ch = *p++) != '\0' && ch != '=')
  1506.     name_len++;
  1507.  
  1508.   if (!ch)
  1509.     abort ();
  1510.  
  1511.   /* Search for replacing an existing environment variable, and
  1512.      count the number of total environment variables.  */
  1513.   for (envp = old_environ; *envp; envp++)
  1514.     {
  1515.       num_envs++;
  1516.       if (!strncmp (str, *envp, name_len))
  1517.     {
  1518.       *envp = str;
  1519.       return;
  1520.     }
  1521.     }
  1522.  
  1523.   /* Add a new environment variable */
  1524.   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
  1525.   *environ = str;
  1526.   bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
  1527.  
  1528. #endif    /* VMS */
  1529. }
  1530.  
  1531. #endif    /* HAVE_PUTENV */
  1532.  
  1533.  
  1534. /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect.  */
  1535.  
  1536. static void
  1537. putenv_from_prefixes (paths, env_var)
  1538.      struct path_prefix *paths;
  1539.      char *env_var;
  1540. {
  1541.   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
  1542.   int just_suffix_len
  1543.     = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
  1544.   int first_time = TRUE;
  1545.   struct prefix_list *pprefix;
  1546.  
  1547.   obstack_grow (&collect_obstack, env_var, strlen (env_var));
  1548.  
  1549.   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
  1550.     {
  1551.       int len = strlen (pprefix->prefix);
  1552.  
  1553.       if (machine_suffix
  1554.       && is_directory (pprefix->prefix, machine_suffix, 0))
  1555.     {
  1556.       if (!first_time)
  1557.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1558.         
  1559.       first_time = FALSE;
  1560.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1561.       obstack_grow (&collect_obstack, machine_suffix, suffix_len);
  1562.     }
  1563.  
  1564.       if (just_machine_suffix
  1565.       && pprefix->require_machine_suffix == 2
  1566.       && is_directory (pprefix->prefix, just_machine_suffix, 0))
  1567.     {
  1568.       if (!first_time)
  1569.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1570.         
  1571.       first_time = FALSE;
  1572.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1573.       obstack_grow (&collect_obstack, just_machine_suffix,
  1574.             just_suffix_len);
  1575.     }
  1576.  
  1577.       if (!pprefix->require_machine_suffix)
  1578.     {
  1579.       if (!first_time)
  1580.         obstack_1grow (&collect_obstack, PATH_SEPARATOR);
  1581.  
  1582.       first_time = FALSE;
  1583.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1584.     }
  1585.     }
  1586.   obstack_1grow (&collect_obstack, '\0');
  1587.   putenv (obstack_finish (&collect_obstack));
  1588. }
  1589. #endif /* ataris */
  1590.  
  1591.  
  1592. /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
  1593.    access to check permissions.
  1594.    Return 0 if not found, otherwise return its name, allocated with malloc. */
  1595.  
  1596. static char *
  1597. find_a_file (pprefix, name, mode)
  1598.      struct path_prefix *pprefix;
  1599.      char *name;
  1600.      int mode;
  1601. {
  1602.   char *temp;
  1603.   char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
  1604.   struct prefix_list *pl;
  1605.   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
  1606.  
  1607.   if (machine_suffix)
  1608.     len += strlen (machine_suffix);
  1609.  
  1610.   temp = xmalloc (len);
  1611.  
  1612.   /* Determine the filename to execute (special case for absolute paths).  */
  1613.  
  1614.   if (*name == '/')
  1615.     {
  1616.       if (access (name, mode))
  1617.     {
  1618.       strcpy (temp, name);
  1619.       return temp;
  1620.     }
  1621.     }
  1622.   else
  1623.     for (pl = pprefix->plist; pl; pl = pl->next)
  1624.       {
  1625.     if (machine_suffix)
  1626.       {
  1627.         strcpy (temp, pl->prefix);
  1628.         strcat (temp, machine_suffix);
  1629.         strcat (temp, name);
  1630.         if (access (temp, mode) == 0)
  1631.           {
  1632.         if (pl->used_flag_ptr != 0)
  1633.           *pl->used_flag_ptr = 1;
  1634.         return temp;
  1635.           }
  1636.         /* Some systems have a suffix for executable files.
  1637.            So try appending that.  */
  1638.         if (file_suffix[0] != 0)
  1639.           {
  1640.         strcat (temp, file_suffix);
  1641.         if (access (temp, mode) == 0)
  1642.           {
  1643.             if (pl->used_flag_ptr != 0)
  1644.               *pl->used_flag_ptr = 1;
  1645.             return temp;
  1646.           }
  1647.           }
  1648.       }
  1649.     /* Certain prefixes are tried with just the machine type,
  1650.        not the version.  This is used for finding as, ld, etc.  */
  1651.     if (just_machine_suffix && pl->require_machine_suffix == 2)
  1652.       {
  1653.         strcpy (temp, pl->prefix);
  1654.         strcat (temp, just_machine_suffix);
  1655.         strcat (temp, name);
  1656.         if (access (temp, mode) == 0)
  1657.           {
  1658.         if (pl->used_flag_ptr != 0)
  1659.           *pl->used_flag_ptr = 1;
  1660.         return temp;
  1661.           }
  1662.         /* Some systems have a suffix for executable files.
  1663.            So try appending that.  */
  1664.         if (file_suffix[0] != 0)
  1665.           {
  1666.         strcat (temp, file_suffix);
  1667.         if (access (temp, mode) == 0)
  1668.           {
  1669.             if (pl->used_flag_ptr != 0)
  1670.               *pl->used_flag_ptr = 1;
  1671.             return temp;
  1672.           }
  1673.           }
  1674.       }
  1675.     /* Certain prefixes can't be used without the machine suffix
  1676.        when the machine or version is explicitly specified.  */
  1677.     if (!pl->require_machine_suffix)
  1678.       {
  1679.         strcpy (temp, pl->prefix);
  1680.         strcat (temp, name);
  1681.         if (access (temp, mode) == 0)
  1682.           {
  1683.         if (pl->used_flag_ptr != 0)
  1684.           *pl->used_flag_ptr = 1;
  1685.         return temp;
  1686.           }
  1687.         /* Some systems have a suffix for executable files.
  1688.            So try appending that.  */
  1689.         if (file_suffix[0] != 0)
  1690.           {
  1691.         strcat (temp, file_suffix);
  1692.         if (access (temp, mode) == 0)
  1693.           {
  1694.             if (pl->used_flag_ptr != 0)
  1695.               *pl->used_flag_ptr = 1;
  1696.             return temp;
  1697.           }
  1698.           }
  1699.       }
  1700.       }
  1701.  
  1702.   free (temp);
  1703.   return 0;
  1704. }
  1705.  
  1706. /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
  1707.    at the start of the list, otherwise it goes at the end.
  1708.  
  1709.    If WARN is nonzero, we will warn if no file is found
  1710.    through this prefix.  WARN should point to an int
  1711.    which will be set to 1 if this entry is used.
  1712.  
  1713.    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
  1714.    the complete value of machine_suffix.
  1715.    2 means try both machine_suffix and just_machine_suffix.  */
  1716.  
  1717. static void
  1718. add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
  1719.      struct path_prefix *pprefix;
  1720.      char *prefix;
  1721.      int first;
  1722.      int require_machine_suffix;
  1723.      int *warn;
  1724. {
  1725.   struct prefix_list *pl, **prev;
  1726.   int len;
  1727.  
  1728.   if (!first && pprefix->plist)
  1729.     {
  1730.       for (pl = pprefix->plist; pl->next; pl = pl->next)
  1731.     ;
  1732.       prev = &pl->next;
  1733.     }
  1734.   else
  1735.     prev = &pprefix->plist;
  1736.  
  1737.   /* Keep track of the longest prefix */
  1738.  
  1739.   len = strlen (prefix);
  1740.   if (len > pprefix->max_len)
  1741.     pprefix->max_len = len;
  1742.  
  1743.   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
  1744.   pl->prefix = save_string (prefix, len);
  1745.   pl->require_machine_suffix = require_machine_suffix;
  1746.   pl->used_flag_ptr = warn;
  1747.   if (warn)
  1748.     *warn = 0;
  1749.  
  1750.   if (*prev)
  1751.     pl->next = *prev;
  1752.   else
  1753.     pl->next = (struct prefix_list *) 0;
  1754.   *prev = pl;
  1755. }
  1756.  
  1757. /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
  1758.  
  1759. static void
  1760. unused_prefix_warnings (pprefix)
  1761.      struct path_prefix *pprefix;
  1762. {
  1763.   struct prefix_list *pl = pprefix->plist;
  1764.  
  1765.   while (pl)
  1766.     {
  1767.       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
  1768.     {
  1769.       error ("file path prefix `%s' never used",
  1770.          pl->prefix);
  1771.       /* Prevent duplicate warnings.  */
  1772.       *pl->used_flag_ptr = 1;
  1773.     }
  1774.       pl = pl->next;
  1775.     }
  1776. }
  1777.  
  1778. /* Get rid of all prefixes built up so far in *PLISTP. */
  1779.  
  1780. static void
  1781. free_path_prefix (pprefix)
  1782.      struct path_prefix *pprefix;
  1783. {
  1784.   struct prefix_list *pl = pprefix->plist;
  1785.   struct prefix_list *temp;
  1786.  
  1787.   while (pl)
  1788.     {
  1789.       temp = pl;
  1790.       pl = pl->next;
  1791.       free (temp->prefix);
  1792.       free ((char *) temp);
  1793.     }
  1794.   pprefix->plist = (struct prefix_list *) 0;
  1795. }
  1796.  
  1797. /* stdin file number.  */
  1798. #define STDIN_FILE_NO 0
  1799.  
  1800. /* stdout file number.  */
  1801. #define STDOUT_FILE_NO 1
  1802.  
  1803. /* value of `pipe': port index for reading.  */
  1804. #define READ_PORT 0
  1805.  
  1806. /* value of `pipe': port index for writing.  */
  1807. #define WRITE_PORT 1
  1808.  
  1809. /* Pipe waiting from last process, to be used as input for the next one.
  1810.    Value is STDIN_FILE_NO if no pipe is waiting
  1811.    (i.e. the next command is the first of a group).  */
  1812.  
  1813. static int last_pipe_input;
  1814.  
  1815. /* Fork one piped subcommand.  FUNC is the system call to use
  1816.    (either execv or execvp).  ARGV is the arg vector to use.
  1817.    NOT_LAST is nonzero if this is not the last subcommand
  1818.    (i.e. its output should be piped to the next one.)  */
  1819.  
  1820. #ifndef OS2
  1821. #ifdef __MSDOS__
  1822.  
  1823. /* Declare these to avoid compilation error.  They won't be called.  */
  1824. int execv(const char *a, const char **b){}
  1825. int execvp(const char *a, const char **b){}
  1826.  
  1827. static int
  1828. pexecute (search_flag, program, argv, not_last)
  1829.      int search_flag;
  1830.      char *program;
  1831.      char *argv[];
  1832.      int not_last;
  1833. {
  1834.   char *scmd, *rf;
  1835.   FILE *argfile;
  1836.   int i, el = search_flag ? 0 : 4;
  1837.  
  1838.   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6 + el);
  1839.   rf = scmd + strlen(program) + 2 + el;
  1840.   sprintf (scmd, "%s%s @%s.gp", program,
  1841.        (search_flag ? "" : ".exe"), temp_filename);
  1842.   argfile = fopen (rf, "w");
  1843.   if (argfile == 0)
  1844.     pfatal_with_name (rf);
  1845.  
  1846.   for (i=1; argv[i]; i++)
  1847.     {
  1848.       char *cp;
  1849.       for (cp = argv[i]; *cp; cp++)
  1850.     {
  1851.       if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
  1852.         fputc ('\\', argfile);
  1853.       fputc (*cp, argfile);
  1854.     }
  1855.       fputc ('\n', argfile);
  1856.     }
  1857.   fclose (argfile);
  1858.  
  1859.   i = system (scmd);
  1860.  
  1861.   remove (rf);
  1862.   
  1863.   if (i == -1)
  1864.     {
  1865.       perror_exec (program);
  1866.       return MIN_FATAL_STATUS << 8;
  1867.     }
  1868.  
  1869.   return i << 8;
  1870. }
  1871.  
  1872. #else /* not __MSDOS__ */
  1873.  
  1874. #ifndef atarist
  1875. static int
  1876. pexecute (search_flag, program, argv, not_last)
  1877.      int search_flag;
  1878.      char *program;
  1879.      char *argv[];
  1880.      int not_last;
  1881. {
  1882.   int (*func)() = (search_flag ? execv : execvp);
  1883.   int pid;
  1884.   int pdes[2];
  1885.   int input_desc = last_pipe_input;
  1886.   int output_desc = STDOUT_FILE_NO;
  1887.   int retries, sleep_interval;
  1888.  
  1889.   /* If this isn't the last process, make a pipe for its output,
  1890.      and record it as waiting to be the input to the next process.  */
  1891.  
  1892.   if (not_last)
  1893.     {
  1894.       if (pipe (pdes) < 0)
  1895.     pfatal_with_name ("pipe");
  1896.       output_desc = pdes[WRITE_PORT];
  1897.       last_pipe_input = pdes[READ_PORT];
  1898.     }
  1899.   else
  1900.     last_pipe_input = STDIN_FILE_NO;
  1901.  
  1902.   /* Fork a subprocess; wait and retry if it fails.  */
  1903.   sleep_interval = 1;
  1904.   for (retries = 0; retries < 4; retries++)
  1905.     {
  1906.       pid = vfork ();
  1907.       if (pid >= 0)
  1908.     break;
  1909.       sleep (sleep_interval);
  1910.       sleep_interval *= 2;
  1911.     }
  1912.  
  1913.   switch (pid)
  1914.     {
  1915.     case -1:
  1916. #ifdef vfork
  1917.       pfatal_with_name ("fork");
  1918. #else
  1919.       pfatal_with_name ("vfork");
  1920. #endif
  1921.       /* NOTREACHED */
  1922.       return 0;
  1923.  
  1924.     case 0: /* child */
  1925.       /* Move the input and output pipes into place, if nec.  */
  1926.       if (input_desc != STDIN_FILE_NO)
  1927.     {
  1928.       close (STDIN_FILE_NO);
  1929.       dup (input_desc);
  1930.       close (input_desc);
  1931.     }
  1932.       if (output_desc != STDOUT_FILE_NO)
  1933.     {
  1934.       close (STDOUT_FILE_NO);
  1935.       dup (output_desc);
  1936.       close (output_desc);
  1937.     }
  1938.  
  1939.       /* Close the parent's descs that aren't wanted here.  */
  1940.       if (last_pipe_input != STDIN_FILE_NO)
  1941.     close (last_pipe_input);
  1942.  
  1943.       /* Exec the program.  */
  1944.       (*func) (program, argv);
  1945.       perror_exec (program);
  1946.       exit (-1);
  1947.       /* NOTREACHED */
  1948.       return 0;
  1949.  
  1950.     default:
  1951.       /* In the parent, after forking.
  1952.      Close the descriptors that we made for this child.  */
  1953.       if (input_desc != STDIN_FILE_NO)
  1954.     close (input_desc);
  1955.       if (output_desc != STDOUT_FILE_NO)
  1956.     close (output_desc);
  1957.  
  1958.       /* Return child's process number.  */
  1959.       return pid;
  1960.     }
  1961. }
  1962.  
  1963. #endif /* not atarist */
  1964. #endif /* not __MSDOS__ */
  1965. #else /* not OS2 */
  1966.  
  1967. static int
  1968. pexecute (search_flag, program, argv, not_last)
  1969.      int search_flag;
  1970.      char *program;
  1971.      char *argv[];
  1972.      int not_last;
  1973. {
  1974.   return (search_flag ? spawnv : spawnvp) (1, program, argv);
  1975. }
  1976. #endif /* not OS2 */
  1977.  
  1978. /* Execute the command specified by the arguments on the current line of spec.
  1979.    When using pipes, this includes several piped-together commands
  1980.    with `|' between them.
  1981.  
  1982.    Return 0 if successful, -1 if failed.  */
  1983.  
  1984. static int
  1985. execute ()
  1986. {
  1987.   int i;
  1988.   int n_commands;        /* # of command.  */
  1989.   char *string;
  1990.   struct command
  1991.     {
  1992.       char *prog;        /* program name.  */
  1993.       char **argv;        /* vector of args.  */
  1994. #ifndef atarist
  1995.       int pid;            /* pid of process for this command.  */
  1996. #endif
  1997.     };
  1998.  
  1999.   struct command *commands;    /* each command buffer with above info.  */
  2000.  
  2001. #ifndef atarist
  2002.   /* Count # of piped commands.  */
  2003.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  2004.     if (strcmp (argbuf[i], "|") == 0)
  2005.       n_commands++;
  2006. #else
  2007.   n_commands = 1;
  2008. #endif
  2009.   /* Get storage for each command.  */
  2010.   commands
  2011.     = (struct command *) alloca (n_commands * sizeof (struct command));
  2012.  
  2013.   /* Split argbuf into its separate piped processes,
  2014.      and record info about each one.
  2015.      Also search for the programs that are to be run.  */
  2016.  
  2017.   commands[0].prog = argbuf[0]; /* first command.  */
  2018.   commands[0].argv = &argbuf[0];
  2019.   string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
  2020.   if (string)
  2021.     commands[0].argv[0] = string;
  2022.  
  2023. #ifndef atarist
  2024.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  2025.     if (strcmp (argbuf[i], "|") == 0)
  2026.       {                /* each command.  */
  2027. #ifdef __MSDOS__
  2028.         fatal ("-pipe not supported under MS-DOS");
  2029. #endif
  2030.     argbuf[i] = 0;    /* termination of command args.  */
  2031.     commands[n_commands].prog = argbuf[i + 1];
  2032.     commands[n_commands].argv = &argbuf[i + 1];
  2033.     string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
  2034.     if (string)
  2035.       commands[n_commands].argv[0] = string;
  2036.     n_commands++;
  2037.       }
  2038. #else
  2039.   n_commands = 1;
  2040. #endif
  2041.  
  2042.   argbuf[argbuf_index] = 0;
  2043.  
  2044.   /* If -v, print what we are about to do, and maybe query.  */
  2045.  
  2046.   if (verbose_flag)
  2047.     {
  2048.       /* Print each piped command as a separate line.  */
  2049.       for (i = 0; i < n_commands ; i++)
  2050.     {
  2051.       char **j;
  2052.  
  2053.       for (j = commands[i].argv; *j; j++)
  2054.         fprintf (stderr, " %s", *j);
  2055.  
  2056. #ifndef atarist
  2057.       /* Print a pipe symbol after all but the last command.  */
  2058.       if (i + 1 != n_commands)
  2059.         fprintf (stderr, " |");
  2060. #endif
  2061.       fprintf (stderr, "\n");
  2062.     }
  2063.       fflush (stderr);
  2064. #ifdef DEBUG
  2065.       fprintf (stderr, "\nGo ahead? (y or n) ");
  2066.       fflush (stderr);
  2067.       i = getchar ();
  2068.       if (i != '\n')
  2069.     while (getchar () != '\n') ;
  2070.       if (i != 'y' && i != 'Y')
  2071.     return 0;
  2072. #endif /* DEBUG */
  2073.     }
  2074.  
  2075. #ifndef atarist
  2076.   /* Run each piped subprocess.  */
  2077.  
  2078.   last_pipe_input = STDIN_FILE_NO;
  2079.   for (i = 0; i < n_commands; i++)
  2080.     {
  2081. #ifndef atariminix
  2082.       char *string = commands[i].argv[0];
  2083.  
  2084.       commands[i].pid = pexecute (string != commands[i].prog,
  2085.                   string, commands[i].argv,
  2086.                   i + 1 < n_commands);
  2087.  
  2088. #else
  2089.       extern int execv();
  2090.       char *string = commands[i].argv[0];
  2091.  
  2092.       commands[i].pid = pexecute (execv,
  2093.                   string, commands[i].argv,
  2094.                   i + 1 < n_commands);
  2095. #endif
  2096.       if (string != commands[i].prog)
  2097.     free (string);
  2098.     }
  2099.  
  2100.   execution_count++;
  2101.  
  2102.   /* Wait for all the subprocesses to finish.
  2103.      We don't care what order they finish in;
  2104.      we know that N_COMMANDS waits will get them all.  */
  2105.  
  2106.   {
  2107.     int ret_code = 0;
  2108.  
  2109.     for (i = 0; i < n_commands; i++)
  2110.       {
  2111.     int status;
  2112.     int pid;
  2113.     char *prog;
  2114.  
  2115. #ifdef __MSDOS__
  2116.         status = pid = commands[i].pid;
  2117. #else
  2118.     pid = wait (&status);
  2119. #endif
  2120.     if (pid < 0)
  2121.       abort ();
  2122.  
  2123.     if (status != 0)
  2124.       {
  2125.         int j;
  2126.         for (j = 0; j < n_commands; j++)
  2127.           if (commands[j].pid == pid)
  2128.         prog = commands[j].prog;
  2129.  
  2130.         if ((status & 0x7F) != 0)
  2131.           {
  2132.         fatal ("Internal compiler error: program %s got fatal signal %d",
  2133.                prog, (status & 0x7F));
  2134.         signal_count++;
  2135.           }
  2136.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  2137.           ret_code = -1;
  2138.       }
  2139.       }
  2140.     return ret_code;
  2141.   }
  2142. #else /* atarist */
  2143.  {
  2144.      register int iii;
  2145.      int errfd, oldfd;
  2146.      char **j;
  2147.  
  2148.      execution_count++;
  2149.  
  2150.      if (zflag)
  2151.        {
  2152.      errfd = Fopen("compile.err", 2);
  2153.      if (errfd < __SMALLEST_VALID_HANDLE)
  2154.        errfd = Fcreate("compile.err", 0);
  2155.      else
  2156.        Fseek(0L, errfd, 2);
  2157.      if (errfd >= __SMALLEST_VALID_HANDLE)
  2158.        {
  2159.          oldfd = Fdup (2);
  2160.          Fforce(2, errfd);
  2161.        }
  2162.      else
  2163.        {
  2164.          error("cannot open %s", "compile.err");
  2165.          zflag = 0;        /* no redirection */
  2166.        }
  2167.        }
  2168.      for (i = 0; i < n_commands ; i++)
  2169.      {
  2170.          j = commands[i].argv;
  2171.          iii = spawnve(0, j[0], j, NULL);
  2172.      if(iii != 0)
  2173.          break;
  2174.      }
  2175.      if (zflag)
  2176.        {
  2177.      Fforce(2, oldfd);
  2178.      Fclose(oldfd);
  2179.      Fclose(errfd);
  2180.        }
  2181.      if (iii != 0)
  2182.        return -1;
  2183.      else
  2184.        return 0;
  2185.  }
  2186. #endif /* atarist */  
  2187. }
  2188.  
  2189. /* Find all the switches given to us
  2190.    and make a vector describing them.
  2191.    The elements of the vector are strings, one per switch given.
  2192.    If a switch uses following arguments, then the `part1' field
  2193.    is the switch itself and the `args' field
  2194.    is a null-terminated vector containing the following arguments.
  2195.    The `valid' field is nonzero if any spec has looked at this switch;
  2196.    if it remains zero at the end of the run, it must be meaningless.  */
  2197.  
  2198. struct switchstr
  2199. {
  2200.   char *part1;
  2201.   char **args;
  2202.   int valid;
  2203. };
  2204.  
  2205. static struct switchstr *switches;
  2206.  
  2207. static int n_switches;
  2208.  
  2209. struct infile
  2210. {
  2211.   char *name;
  2212.   char *language;
  2213. };
  2214.  
  2215. /* Also a vector of input files specified.  */
  2216.  
  2217. static struct infile *infiles;
  2218.  
  2219. static int n_infiles;
  2220.  
  2221. /* And a vector of corresponding output files is made up later.  */
  2222.  
  2223. static char **outfiles;
  2224.  
  2225. /* Create the vector `switches' and its contents.
  2226.    Store its length in `n_switches'.  */
  2227.  
  2228. static void
  2229. process_command (argc, argv)
  2230.      int argc;
  2231.      char **argv;
  2232. {
  2233.   register int i;
  2234.   char *temp;
  2235.   char *spec_lang = 0;
  2236.   int last_language_n_infiles;
  2237.  
  2238.   gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  2239.  
  2240.   n_switches = 0;
  2241.   n_infiles = 0;
  2242.  
  2243.   /* Default for -V is our version number, ending at first space.  */
  2244.   spec_version = save_string (version_string, strlen (version_string));
  2245.   for (temp = spec_version; *temp && *temp != ' '; temp++);
  2246.   if (*temp) *temp = '\0';
  2247.  
  2248.   /* Set up the default search paths.  */
  2249.  
  2250. #ifndef CROSSATARI
  2251.   if (gcc_exec_prefix)
  2252.     {
  2253.       add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
  2254.       add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
  2255.     }
  2256.  
  2257.   /* COMPILER_PATH and LIBRARY_PATH have values
  2258.      that are lists of directory names with colons.  */
  2259.  
  2260.   temp = getenv ("COMPILER_PATH");
  2261.   if (temp)
  2262.     {
  2263.       char *startp, *endp;
  2264.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2265.  
  2266.       startp = endp = temp;
  2267.       while (1)
  2268.     {
  2269.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2270.         {
  2271.           strncpy (nstore, startp, endp-startp);
  2272.           if (endp == startp)
  2273.         {
  2274.           strcpy (nstore, "./");
  2275.         }
  2276.           else if (endp[-1] != '/')
  2277.         {
  2278.           nstore[endp-startp] = '/';
  2279.           nstore[endp-startp+1] = 0;
  2280.         }
  2281.           else
  2282.         nstore[endp-startp] = 0;
  2283.           add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
  2284.           if (*endp == 0)
  2285.         break;
  2286.           endp = startp = endp + 1;
  2287.         }
  2288.       else
  2289.         endp++;
  2290.     }
  2291.     }
  2292.  
  2293.   temp = getenv ("LIBRARY_PATH");
  2294.   if (temp)
  2295.     {
  2296.       char *startp, *endp;
  2297.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2298.  
  2299.       startp = endp = temp;
  2300.       while (1)
  2301.     {
  2302.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2303.         {
  2304.           strncpy (nstore, startp, endp-startp);
  2305.           if (endp == startp)
  2306.         {
  2307.           strcpy (nstore, "./");
  2308.         }
  2309.           else if (endp[-1] != '/')
  2310.         {
  2311.           nstore[endp-startp] = '/';
  2312.           nstore[endp-startp+1] = 0;
  2313.         }
  2314.           else
  2315.         nstore[endp-startp] = 0;
  2316.           add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
  2317.           if (*endp == 0)
  2318.         break;
  2319.           endp = startp = endp + 1;
  2320.         }
  2321.       else
  2322.         endp++;
  2323.     }
  2324.     }
  2325. #endif /* CROSSATARI */
  2326.  
  2327.   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
  2328.   temp = getenv ("LPATH");
  2329.   if (temp)
  2330.     {
  2331.       char *startp, *endp;
  2332.       char *nstore = (char *) alloca (strlen (temp) + 3);
  2333.  
  2334.       startp = endp = temp;
  2335.       while (1)
  2336.     {
  2337.       if (*endp == PATH_SEPARATOR || *endp == 0)
  2338.         {
  2339.           strncpy (nstore, startp, endp-startp);
  2340.           if (endp == startp)
  2341.         {
  2342.           strcpy (nstore, "./");
  2343.         }
  2344.           else if (endp[-1] != '/')
  2345.         {
  2346.           nstore[endp-startp] = '/';
  2347.           nstore[endp-startp+1] = 0;
  2348.         }
  2349.           else
  2350.         nstore[endp-startp] = 0;
  2351.           add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
  2352.           if (*endp == 0)
  2353.         break;
  2354.           endp = startp = endp + 1;
  2355.         }
  2356.       else
  2357.         endp++;
  2358.     }
  2359.     }
  2360.  
  2361.   /* Convert new-style -- options to old-style.  */
  2362.   translate_options (&argc, &argv);
  2363.  
  2364.   /* Scan argv twice.  Here, the first time, just count how many switches
  2365.      there will be in their vector, and how many input files in theirs.
  2366.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  2367.  
  2368.   for (i = 1; i < argc; i++)
  2369.     {
  2370.       if (! strcmp (argv[i], "-dumpspecs"))
  2371.     {
  2372.       printf ("*asm:\n%s\n\n", asm_spec);
  2373.       printf ("*asm_final:\n%s\n\n", asm_final_spec);
  2374.       printf ("*cpp:\n%s\n\n", cpp_spec);
  2375.       printf ("*cc1:\n%s\n\n", cc1_spec);
  2376.       printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
  2377.       printf ("*endfile:\n%s\n\n", endfile_spec);
  2378.       printf ("*link:\n%s\n\n", link_spec);
  2379.       printf ("*lib:\n%s\n\n", lib_spec);
  2380.       printf ("*startfile:\n%s\n\n", startfile_spec);
  2381.       printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
  2382.       printf ("*signed_char:\n%s\n\n", signed_char_spec);
  2383.       printf ("*predefines:\n%s\n\n", cpp_predefines);
  2384.       printf ("*cross_compile:\n%d\n\n", cross_compile);
  2385.  
  2386.       exit (0);
  2387.     }
  2388.       else if (! strcmp (argv[i], "-dumpversion"))
  2389.     {
  2390.       printf ("%s\n", version_string);
  2391.       exit (0);
  2392.     }
  2393.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2394.     {
  2395.       print_libgcc_file_name = 1;
  2396.     }
  2397.       else if (! strcmp (argv[i], "-Xlinker"))
  2398.     {
  2399.       /* Pass the argument of this option to the linker when we link.  */
  2400.  
  2401.       if (i + 1 == argc)
  2402.         fatal ("argument to `-Xlinker' is missing");
  2403.  
  2404.       n_linker_options++;
  2405.       if (!linker_options)
  2406.         linker_options
  2407.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  2408.       else
  2409.         linker_options
  2410.           = (char **) xrealloc (linker_options,
  2411.                     n_linker_options * sizeof (char **));
  2412.  
  2413.       linker_options[n_linker_options - 1] = argv[++i];
  2414.     }
  2415.       else if (! strncmp (argv[i], "-Wl,", 4))
  2416.     {
  2417.       int prev, j;
  2418.       /* Pass the rest of this option to the linker when we link.  */
  2419.  
  2420.       n_linker_options++;
  2421.       if (!linker_options)
  2422.         linker_options
  2423.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  2424.       else
  2425.         linker_options
  2426.           = (char **) xrealloc (linker_options,
  2427.                     n_linker_options * sizeof (char **));
  2428.  
  2429.       /* Split the argument at commas.  */
  2430.       prev = 4;
  2431.       for (j = 4; argv[i][j]; j++)
  2432.         if (argv[i][j] == ',')
  2433.           {
  2434.         linker_options[n_linker_options - 1]
  2435.           = save_string (argv[i] + prev, j - prev);
  2436.         n_linker_options++;
  2437.         linker_options
  2438.           = (char **) xrealloc (linker_options,
  2439.                     n_linker_options * sizeof (char **));
  2440.         prev = j + 1;
  2441.           }
  2442.       /* Record the part after the last comma.  */
  2443.       linker_options[n_linker_options - 1] = argv[i] + prev;
  2444.     }
  2445.       else if (! strncmp (argv[i], "-Wa,", 4))
  2446.     {
  2447.       int prev, j;
  2448.       /* Pass the rest of this option to the assembler.  */
  2449.  
  2450.       n_assembler_options++;
  2451.       if (!assembler_options)
  2452.         assembler_options
  2453.           = (char **) xmalloc (n_assembler_options * sizeof (char **));
  2454.       else
  2455.         assembler_options
  2456.           = (char **) xrealloc (assembler_options,
  2457.                     n_assembler_options * sizeof (char **));
  2458.  
  2459.       /* Split the argument at commas.  */
  2460.       prev = 4;
  2461.       for (j = 4; argv[i][j]; j++)
  2462.         if (argv[i][j] == ',')
  2463.           {
  2464.         assembler_options[n_assembler_options - 1]
  2465.           = save_string (argv[i] + prev, j - prev);
  2466.         n_assembler_options++;
  2467.         assembler_options
  2468.           = (char **) xrealloc (assembler_options,
  2469.                     n_assembler_options * sizeof (char **));
  2470.         prev = j + 1;
  2471.           }
  2472.       /* Record the part after the last comma.  */
  2473.       assembler_options[n_assembler_options - 1] = argv[i] + prev;
  2474.     }
  2475.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2476.     /* The +e options to the C++ front-end.  */
  2477.     n_switches++;
  2478.       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
  2479.     {
  2480.       register char *p = &argv[i][1];
  2481.       register int c = *p;
  2482.  
  2483.       switch (c)
  2484.         {
  2485.         case 'b':
  2486.           if (p[1] == 0 && i + 1 == argc)
  2487.         fatal ("argument to `-b' is missing");
  2488.           if (p[1] == 0)
  2489.         spec_machine = argv[++i];
  2490.           else
  2491.         spec_machine = p + 1;
  2492.           break;
  2493.  
  2494.         case 'B':
  2495.           {
  2496.         int *temp = (int *) xmalloc (sizeof (int));
  2497.         char *value;
  2498.         if (p[1] == 0 && i + 1 == argc)
  2499.           fatal ("argument to `-B' is missing");
  2500.         if (p[1] == 0)
  2501.           value = argv[++i];
  2502.         else
  2503.           value = p + 1;
  2504.         add_prefix (&exec_prefix, value, 1, 0, temp);
  2505.         add_prefix (&startfile_prefix, value, 1, 0, temp);
  2506.           }
  2507.           break;
  2508.  
  2509.         case 'v':    /* Print our subcommands and print versions.  */
  2510.           n_switches++;
  2511.           /* If they do anything other than exactly `-v', don't set
  2512.          verbose_flag; rather, continue on to give the error.  */
  2513.           if (p[1] != 0)
  2514.         break;
  2515.           verbose_flag++;
  2516.           break;
  2517.  
  2518.         case 'V':
  2519.           if (p[1] == 0 && i + 1 == argc)
  2520.         fatal ("argument to `-V' is missing");
  2521.           if (p[1] == 0)
  2522.         spec_version = argv[++i];
  2523.           else
  2524.         spec_version = p + 1;
  2525.           break;
  2526.  
  2527.         case 's':
  2528.           if (!strcmp (p, "save-temps"))
  2529.         {
  2530.           save_temps_flag = 1;
  2531.           n_switches++;
  2532.           break;
  2533.         }
  2534. #ifdef atarist
  2535.         case 'z':    /* redirect stderr to a file */
  2536.           zflag++;
  2537.           n_switches++;
  2538.           break;
  2539. #endif
  2540.         default:
  2541.           n_switches++;
  2542.  
  2543.           if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2544.         i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2545.           else if (WORD_SWITCH_TAKES_ARG (p))
  2546.         i += WORD_SWITCH_TAKES_ARG (p);
  2547.         }
  2548.     }
  2549.       else
  2550.     n_infiles++;
  2551.     }
  2552.  
  2553.   /* Set up the search paths before we go looking for config files.  */
  2554.  
  2555.   /* These come before the md prefixes so that we will find gcc's subcommands
  2556.      (such as cpp) rather than those of the host system.  */
  2557.   /* Use 2 as fourth arg meaning try just the machine as a suffix,
  2558.      as well as trying the machine and the version.  */
  2559. #if (defined(atarist) || defined(CROSSATARI) || defined(WEIRD))
  2560. /* NB defined(WEIRD) should not really be there, its only for my weird setup */
  2561.   /* We are not using machine_suffix here, so do not make */
  2562.   /* these prefixes available only when we have one       */
  2563.   add_prefix (&exec_prefix, standard_exec_prefix, 0, 0, NULL_PTR);
  2564.   add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 0, NULL_PTR);
  2565.  
  2566.   add_prefix (&startfile_prefix, standard_exec_prefix, 0, 0, NULL_PTR);
  2567.   add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 0, NULL_PTR);
  2568. #else
  2569.   add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
  2570.   add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
  2571.  
  2572.   add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
  2573.   add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
  2574. #endif /* (defined(atarist) || defined(CROSSATARI)) */
  2575.  
  2576.   tooldir_prefix = concat (tooldir_base_prefix, spec_machine, "/");
  2577.  
  2578.   /* If tooldir is relative, base it on exec_prefix.  A relative
  2579.      tooldir lets us move the installed tree as a unit.
  2580.  
  2581.      If GCC_EXEC_PREFIX is defined, then we want to add two relative
  2582.      directories, so that we can search both the user specified directory
  2583.      and the standard place.  */
  2584.  
  2585.   if (*tooldir_prefix != '/')
  2586.     {
  2587.       if (gcc_exec_prefix)
  2588.     {
  2589.       char *gcc_exec_tooldir_prefix
  2590.         = concat (concat (gcc_exec_prefix, spec_machine, "/"),
  2591.               concat (spec_version, "/", tooldir_prefix),
  2592.               "");
  2593.  
  2594.       add_prefix (&exec_prefix, concat (gcc_exec_tooldir_prefix, "bin", "/"),
  2595.               0, 0, NULL_PTR);
  2596.       add_prefix (&startfile_prefix, concat (gcc_exec_tooldir_prefix, "lib", "/"),
  2597.               0, 0, NULL_PTR);
  2598.     }
  2599.  
  2600.       tooldir_prefix = concat (concat (standard_exec_prefix, spec_machine, "/"),
  2601.                    concat (spec_version, "/", tooldir_prefix),
  2602.                    "");
  2603.     }
  2604.  
  2605.   add_prefix (&exec_prefix, concat (tooldir_prefix, "bin", "/"),
  2606.           0, 0, NULL_PTR);
  2607.   add_prefix (&startfile_prefix, concat (tooldir_prefix, "lib", "/"),
  2608.           0, 0, NULL_PTR);
  2609.  
  2610.   /* More prefixes are enabled in main, after we read the specs file
  2611.      and determine whether this is cross-compilation or not.  */
  2612.  
  2613.  
  2614.   /* Then create the space for the vectors and scan again.  */
  2615.  
  2616.   switches = ((struct switchstr *)
  2617.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  2618.   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
  2619.   n_switches = 0;
  2620.   n_infiles = 0;
  2621.   last_language_n_infiles = -1;
  2622.  
  2623.   /* This, time, copy the text of each switch and store a pointer
  2624.      to the copy in the vector of switches.
  2625.      Store all the infiles in their vector.  */
  2626.  
  2627.   for (i = 1; i < argc; i++)
  2628.     {
  2629.       /* Just skip the switches that were handled by the preceding loop.  */
  2630.       if (!strcmp (argv[i], "-Xlinker"))
  2631.     i++;
  2632.       else if (! strncmp (argv[i], "-Wl,", 4))
  2633.     ;
  2634.       else if (! strncmp (argv[i], "-Wa,", 4))
  2635.     ;
  2636.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2637.     ;
  2638.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2639.     {
  2640.       /* Compensate for the +e options to the C++ front-end;
  2641.          they're there simply for cfront call-compatibility.  We do
  2642.          some magic in default_compilers to pass them down properly.
  2643.          Note we deliberately start at the `+' here, to avoid passing
  2644.          -e0 or -e1 down into the linker.  */
  2645.       switches[n_switches].part1 = &argv[i][0];
  2646.       switches[n_switches].args = 0;
  2647.       switches[n_switches].valid = 0;
  2648.       n_switches++;
  2649.     }
  2650.       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
  2651.     {
  2652.       register char *p = &argv[i][1];
  2653.       register int c = *p;
  2654.  
  2655.       if (c == 'B' || c == 'b' || c == 'V')
  2656.         {
  2657.           /* Skip a separate arg, if any.  */
  2658.           if (p[1] == 0)
  2659.         i++;
  2660.           continue;
  2661.         }
  2662.       if (c == 'x')
  2663.         {
  2664.           if (p[1] == 0 && i + 1 == argc)
  2665.         fatal ("argument to `-x' is missing");
  2666.           if (p[1] == 0)
  2667.         spec_lang = argv[++i];
  2668.           else
  2669.         spec_lang = p + 1;
  2670.           if (! strcmp (spec_lang, "none"))
  2671.         /* Suppress the warning if -xnone comes after the last input file,
  2672.            because alternate command interfaces like g++ might find it
  2673.            useful to place -xnone after each input file.  */
  2674.         spec_lang = 0;
  2675.           else
  2676.         last_language_n_infiles = n_infiles;
  2677.           continue;
  2678.         }
  2679.       switches[n_switches].part1 = p;
  2680.       /* Deal with option arguments in separate argv elements.  */
  2681.       if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2682.           || WORD_SWITCH_TAKES_ARG (p))
  2683.         {
  2684.           int j = 0;
  2685.           int n_args = WORD_SWITCH_TAKES_ARG (p);
  2686.  
  2687.           if (n_args == 0)
  2688.         {
  2689.           /* Count only the option arguments in separate argv elements.  */
  2690.           n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2691.         }
  2692.           if (i + n_args >= argc)
  2693.         fatal ("argument to `-%s' is missing", p);
  2694.           switches[n_switches].args
  2695.         = (char **) xmalloc ((n_args + 1) * sizeof (char *));
  2696.           while (j < n_args)
  2697.         switches[n_switches].args[j++] = argv[++i];
  2698.           /* Null-terminate the vector.  */
  2699.           switches[n_switches].args[j] = 0;
  2700.         }
  2701.       else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L'))
  2702.         {
  2703.           /* On some systems, ld cannot handle -o or -L without space.
  2704.          So split the -o or -L from its argument.  */
  2705.           switches[n_switches].part1 = (c == 'o' ? "o" : "L");
  2706.           switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
  2707.           switches[n_switches].args[0] = xmalloc (strlen (p));
  2708.           strcpy (switches[n_switches].args[0], &p[1]);
  2709.           switches[n_switches].args[1] = 0;
  2710.         }
  2711.       else
  2712.         switches[n_switches].args = 0;
  2713.       switches[n_switches].valid = 0;
  2714.       /* This is always valid, since gcc.c itself understands it.  */
  2715.       if (!strcmp (p, "save-temps"))
  2716.         switches[n_switches].valid = 1;
  2717.       n_switches++;
  2718.     }
  2719.       else
  2720.     {
  2721.       if ((argv[i][0] != '-' || argv[i][1] != 'l')
  2722.           && strcmp (argv[i], "-")
  2723.           && access (argv[i], R_OK) < 0)
  2724.         {
  2725.           perror_with_name (argv[i]);
  2726.           error_count++;
  2727.         }
  2728.       else
  2729.         {
  2730.           infiles[n_infiles].language = spec_lang;
  2731.           infiles[n_infiles++].name = argv[i];
  2732.         }
  2733.     }
  2734.     }
  2735.  
  2736.   if (n_infiles == last_language_n_infiles && spec_lang != 0)
  2737.     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
  2738.  
  2739.   switches[n_switches].part1 = 0;
  2740.   infiles[n_infiles].name = 0;
  2741.  
  2742.   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
  2743.   if (gcc_exec_prefix)
  2744.     {
  2745.       temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
  2746.                    + strlen (spec_machine) + 3);
  2747.       strcpy (temp, gcc_exec_prefix);
  2748.       strcat (temp, spec_machine);
  2749.       strcat (temp, "/");
  2750.       strcat (temp, spec_version);
  2751.       strcat (temp, "/");
  2752.       gcc_exec_prefix = temp;
  2753.     }
  2754. }
  2755.  
  2756. /* Process a spec string, accumulating and running commands.  */
  2757.  
  2758. /* These variables describe the input file name.
  2759.    input_file_number is the index on outfiles of this file,
  2760.    so that the output file name can be stored for later use by %o.
  2761.    input_basename is the start of the part of the input file
  2762.    sans all directory names, and basename_length is the number
  2763.    of characters starting there excluding the suffix .c or whatever.  */
  2764.  
  2765. static char *input_filename;
  2766. static int input_file_number;
  2767. static int input_filename_length;
  2768. static int basename_length;
  2769. static char *input_basename;
  2770. static char *input_suffix;
  2771.  
  2772. /* These are variables used within do_spec and do_spec_1.  */
  2773.  
  2774. /* Nonzero if an arg has been started and not yet terminated
  2775.    (with space, tab or newline).  */
  2776. static int arg_going;
  2777.  
  2778. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  2779.    is a temporary file name.  */
  2780. static int delete_this_arg;
  2781.  
  2782. /* Nonzero means %w has been seen; the next arg to be terminated
  2783.    is the output file name of this compilation.  */
  2784. static int this_is_output_file;
  2785.  
  2786. /* Nonzero means %s has been seen; the next arg to be terminated
  2787.    is the name of a library file and we should try the standard
  2788.    search dirs for it.  */
  2789. static int this_is_library_file;
  2790.  
  2791. /* Nonzero means that the input of this command is coming from a pipe.  */
  2792. static int input_from_pipe;
  2793.  
  2794. /* Process the spec SPEC and run the commands specified therein.
  2795.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  2796.  
  2797. static int
  2798. do_spec (spec)
  2799.      char *spec;
  2800. {
  2801.   int value;
  2802.  
  2803.   clear_args ();
  2804.   arg_going = 0;
  2805.   delete_this_arg = 0;
  2806.   this_is_output_file = 0;
  2807.   this_is_library_file = 0;
  2808.   input_from_pipe = 0;
  2809.  
  2810.   value = do_spec_1 (spec, 0, NULL_PTR);
  2811.  
  2812.   /* Force out any unfinished command.
  2813.      If -pipe, this forces out the last command if it ended in `|'.  */
  2814.   if (value == 0)
  2815.     {
  2816. #ifndef atarist
  2817.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  2818.     argbuf_index--;
  2819. #endif
  2820.  
  2821.       if (argbuf_index > 0)
  2822.     value = execute ();
  2823.     }
  2824.  
  2825.   return value;
  2826. }
  2827.  
  2828. /* Process the sub-spec SPEC as a portion of a larger spec.
  2829.    This is like processing a whole spec except that we do
  2830.    not initialize at the beginning and we do not supply a
  2831.    newline by default at the end.
  2832.    INSWITCH nonzero means don't process %-sequences in SPEC;
  2833.    in this case, % is treated as an ordinary character.
  2834.    This is used while substituting switches.
  2835.    INSWITCH nonzero also causes SPC not to terminate an argument.
  2836.  
  2837.    Value is zero unless a line was finished
  2838.    and the command on that line reported an error.  */
  2839.  
  2840. static int
  2841. do_spec_1 (spec, inswitch, soft_matched_part)
  2842.      char *spec;
  2843.      int inswitch;
  2844.      char *soft_matched_part;
  2845. {
  2846.   register char *p = spec;
  2847.   register int c;
  2848.   int i;
  2849.   char *string;
  2850.   int value;
  2851.  
  2852.   while (c = *p++)
  2853.     /* If substituting a switch, treat all chars like letters.
  2854.        Otherwise, NL, SPC, TAB and % are special.  */
  2855.     switch (inswitch ? 'a' : c)
  2856.       {
  2857. #if (defined(atarist) || defined(CROSSATARI))
  2858. /* this stuff added by jrd.  if see '$', expect name of env var, delimited
  2859.    by '$'.  Find it's value, and subst it in.
  2860.  
  2861.    modified by ERS to only collect things that look like names; this
  2862.    saves e.g. the -$ arg. to cpp from getting munged */
  2863.  
  2864.       case '$':
  2865.       {
  2866.     char varname[32];        /* should be enough */
  2867.     char * value;            /* deciphered value string */
  2868.     char *temp;
  2869.     int i;
  2870.     extern char *getenv();
  2871.     
  2872.     for (i = 0 ; ((c = *p) != '$') && isalnum(c) && i < 31 ; i++, p++)
  2873.         varname[i] = c;
  2874.     varname[i] = '\0';
  2875.     if (i > 0) /* ++jrb fix */
  2876.     {
  2877.                  ++p;    /* skip trailing '$' */
  2878.         value = getenv(varname); /* ++jrb fix */
  2879.         if (value)
  2880.         { /* if value is a path, only consider 1'st component */
  2881. #ifdef atarist
  2882.             if((temp = index(value, ',')) == NULL)
  2883.                 temp = index(value, ';');
  2884. #else
  2885.             temp = index(value, ':');
  2886. #endif
  2887.             obstack_grow (&obstack, value,
  2888.                       temp ? temp - value : strlen (value));
  2889.         }
  2890.         else
  2891.           obstack_1grow (&obstack, '.'); /* a complete kludge... */
  2892.     }
  2893.         else
  2894.         {
  2895.              obstack_1grow(&obstack, '$');
  2896.         }
  2897.     arg_going = 1;
  2898.       }
  2899.      break;
  2900. #endif /* atarist */
  2901.       case '\n':
  2902.     /* End of line: finish any pending argument,
  2903.        then run the pending command if one has been started.  */
  2904.     if (arg_going)
  2905.       {
  2906.         obstack_1grow (&obstack, 0);
  2907.         string = obstack_finish (&obstack);
  2908.         if (this_is_library_file)
  2909.           string = find_file (string);
  2910.         store_arg (string, delete_this_arg, this_is_output_file);
  2911.         if (this_is_output_file)
  2912.           outfiles[input_file_number] = string;
  2913.       }
  2914.     arg_going = 0;
  2915.  
  2916.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  2917.       {
  2918.         int i;
  2919.         for (i = 0; i < n_switches; i++)
  2920.           if (!strcmp (switches[i].part1, "pipe"))
  2921.         break;
  2922.  
  2923.         /* A `|' before the newline means use a pipe here,
  2924.            but only if -pipe was specified.
  2925.            Otherwise, execute now and don't pass the `|' as an arg.  */
  2926.         if (i < n_switches)
  2927.           {
  2928.         input_from_pipe = 1;
  2929.         switches[i].valid = 1;
  2930.         break;
  2931.           }
  2932.         else
  2933.           argbuf_index--;
  2934.       }
  2935.  
  2936.     if (argbuf_index > 0)
  2937.       {
  2938.         value = execute ();
  2939.         if (value)
  2940.           return value;
  2941.       }
  2942.     /* Reinitialize for a new command, and for a new argument.  */
  2943.     clear_args ();
  2944.     arg_going = 0;
  2945.     delete_this_arg = 0;
  2946.     this_is_output_file = 0;
  2947.     this_is_library_file = 0;
  2948.     input_from_pipe = 0;
  2949.     break;
  2950.  
  2951.       case '|':
  2952.     /* End any pending argument.  */
  2953.     if (arg_going)
  2954.       {
  2955.         obstack_1grow (&obstack, 0);
  2956.         string = obstack_finish (&obstack);
  2957.         if (this_is_library_file)
  2958.           string = find_file (string);
  2959.         store_arg (string, delete_this_arg, this_is_output_file);
  2960.         if (this_is_output_file)
  2961.           outfiles[input_file_number] = string;
  2962.       }
  2963.  
  2964.     /* Use pipe */
  2965.     obstack_1grow (&obstack, c);
  2966.     arg_going = 1;
  2967.     break;
  2968.  
  2969.       case '\t':
  2970.       case ' ':
  2971.     /* Space or tab ends an argument if one is pending.  */
  2972.     if (arg_going)
  2973.       {
  2974.         obstack_1grow (&obstack, 0);
  2975.         string = obstack_finish (&obstack);
  2976.         if (this_is_library_file)
  2977.           string = find_file (string);
  2978.         store_arg (string, delete_this_arg, this_is_output_file);
  2979.         if (this_is_output_file)
  2980.           outfiles[input_file_number] = string;
  2981.       }
  2982.     /* Reinitialize for a new argument.  */
  2983.     arg_going = 0;
  2984.     delete_this_arg = 0;
  2985.     this_is_output_file = 0;
  2986.     this_is_library_file = 0;
  2987.     break;
  2988.  
  2989.       case '%':
  2990.     switch (c = *p++)
  2991.       {
  2992.       case 0:
  2993.         fatal ("Invalid specification!  Bug in cc.");
  2994.  
  2995.       case 'b':
  2996.         obstack_grow (&obstack, input_basename, basename_length);
  2997.         arg_going = 1;
  2998.         break;
  2999.  
  3000.       case 'd':
  3001.         delete_this_arg = 2;
  3002.         break;
  3003.  
  3004.       /* Dump out the directories specified with LIBRARY_PATH,
  3005.          followed by the absolute directories
  3006.          that we search for startfiles.  */
  3007.       case 'D':
  3008.         {
  3009.           struct prefix_list *pl = startfile_prefix.plist;
  3010.           int bufsize = 100;
  3011.           char *buffer = (char *) xmalloc (bufsize);
  3012.           int idx;
  3013.  
  3014.           for (; pl; pl = pl->next)
  3015.         {
  3016. #ifdef RELATIVE_PREFIX_NOT_LINKDIR
  3017.           /* Used on systems which record the specified -L dirs
  3018.              and use them to search for dynamic linking.  */
  3019.           /* Relative directories always come from -B,
  3020.              and it is better not to use them for searching
  3021.              at run time.  In particular, stage1 loses  */
  3022.           if (pl->prefix[0] != '/')
  3023.             continue;
  3024. #endif
  3025.           if (machine_suffix)
  3026.             {
  3027.               if (is_directory (pl->prefix, machine_suffix, 1))
  3028.             {
  3029.               do_spec_1 ("-L", 0, NULL_PTR);
  3030. #ifdef SPACE_AFTER_L_OPTION
  3031.               do_spec_1 (" ", 0, NULL_PTR);
  3032. #endif
  3033.               do_spec_1 (pl->prefix, 1, NULL_PTR);
  3034.               /* Remove slash from machine_suffix.  */
  3035.               if (strlen (machine_suffix) >= bufsize)
  3036.                 bufsize = strlen (machine_suffix) * 2 + 1;
  3037.               buffer = (char *) xrealloc (buffer, bufsize);
  3038.               strcpy (buffer, machine_suffix);
  3039.               idx = strlen (buffer);
  3040.               if (buffer[idx - 1] == '/')
  3041.                 buffer[idx - 1] = 0;
  3042.               do_spec_1 (buffer, 1, NULL_PTR);
  3043.               /* Make this a separate argument.  */
  3044.               do_spec_1 (" ", 0, NULL_PTR);
  3045.             }
  3046.             }
  3047.           if (!pl->require_machine_suffix)
  3048.             {
  3049.               if (is_directory (pl->prefix, "", 1))
  3050.             {
  3051.               do_spec_1 ("-L", 0, NULL_PTR);
  3052. #ifdef SPACE_AFTER_L_OPTION
  3053.               do_spec_1 (" ", 0, NULL_PTR);
  3054. #endif
  3055.               /* Remove slash from pl->prefix.  */
  3056.               if (strlen (pl->prefix) >= bufsize)
  3057.                 bufsize = strlen (pl->prefix) * 2 + 1;
  3058.               buffer = (char *) xrealloc (buffer, bufsize);
  3059.               strcpy (buffer, pl->prefix);
  3060.               idx = strlen (buffer);
  3061.               if (buffer[idx - 1] == '/')
  3062.                 buffer[idx - 1] = 0;
  3063.               do_spec_1 (buffer, 1, NULL_PTR);
  3064.               /* Make this a separate argument.  */
  3065.               do_spec_1 (" ", 0, NULL_PTR);
  3066.             }
  3067.             }
  3068.         }
  3069.           free (buffer);
  3070.         }
  3071.         break;
  3072.  
  3073.       case 'e':
  3074.         /* {...:%efoo} means report an error with `foo' as error message
  3075.            and don't execute any more commands for this file.  */
  3076.         {
  3077.           char *q = p;
  3078.           char *buf;
  3079.           while (*p != 0 && *p != '\n') p++;
  3080.           buf = (char *) alloca (p - q + 1);
  3081.           strncpy (buf, q, p - q);
  3082.           buf[p - q] = 0;
  3083.           error ("%s", buf);
  3084.           return -1;
  3085.         }
  3086.         break;
  3087.  
  3088.       case 'g':
  3089.       case 'u':
  3090.       case 'U':
  3091.         if (save_temps_flag)
  3092.           obstack_grow (&obstack, input_basename, basename_length);
  3093.         else
  3094.           {
  3095. #ifdef MKTEMP_EACH_FILE
  3096.         /* ??? This has a problem: the total number of
  3097.            values mktemp can return is limited.
  3098.            That matters for the names of object files.
  3099.            In 2.4, do something about that.  */
  3100.         struct temp_name *t;
  3101.         char *suffix = p;
  3102.         while (*p == '.' || isalpha (*p))
  3103.           p++;
  3104.  
  3105.         /* See if we already have an association of %g/%u/%U and
  3106.            suffix.  */
  3107.         for (t = temp_names; t; t = t->next)
  3108.           if (t->length == p - suffix
  3109.               && strncmp (t->suffix, suffix, p - suffix) == 0
  3110.               && t->unique == (c != 'g'))
  3111.             break;
  3112.  
  3113.         /* Make a new association if needed.  %u requires one.  */
  3114.         if (t == 0 || c == 'u')
  3115.           {
  3116.             if (t == 0)
  3117.               {
  3118.             t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
  3119.             t->next = temp_names;
  3120.             temp_names = t;
  3121.               }
  3122.             t->length = p - suffix;
  3123.             t->suffix = save_string (suffix, p - suffix);
  3124.             t->unique = (c != 'g');
  3125.             choose_temp_base ();
  3126.             t->filename = temp_filename;
  3127.             t->filename_length = temp_filename_length;
  3128.           }
  3129.  
  3130.         obstack_grow (&obstack, t->filename, t->filename_length);
  3131.         delete_this_arg = 1;
  3132. #else
  3133.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  3134.         if (c == 'u' || c == 'U')
  3135.           {
  3136.             static int unique;
  3137.             char buff[9];
  3138.             if (c == 'u')
  3139.               unique++;
  3140.             sprintf (buff, "%d", unique);
  3141.             obstack_grow (&obstack, buff, strlen (buff));
  3142.           }
  3143. #endif
  3144.         delete_this_arg = 1;
  3145.           }
  3146.         arg_going = 1;
  3147.         break;
  3148.  
  3149.       case 'i':
  3150.         obstack_grow (&obstack, input_filename, input_filename_length);
  3151.         arg_going = 1;
  3152.         break;
  3153.  
  3154.       case 'I':
  3155.         if (gcc_exec_prefix)
  3156.           {
  3157.         do_spec_1 ("-iprefix", 1, NULL_PTR);
  3158.         /* Make this a separate argument.  */
  3159.         do_spec_1 (" ", 0, NULL_PTR);
  3160.         do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
  3161.         do_spec_1 (" ", 0, NULL_PTR);
  3162.           }
  3163.         break;
  3164.  
  3165.       case 'o':
  3166.         {
  3167.           register int f;
  3168.           for (f = 0; f < n_infiles; f++)
  3169.         store_arg (outfiles[f], 0, 0);
  3170.         }
  3171.         break;
  3172.  
  3173.       case 's':
  3174.         this_is_library_file = 1;
  3175.         break;
  3176.  
  3177.       case 'w':
  3178.         this_is_output_file = 1;
  3179.         break;
  3180.  
  3181.       case 'W':
  3182.         {
  3183.           int index = argbuf_index;
  3184.           /* Handle the {...} following the %W.  */
  3185.           if (*p != '{')
  3186.         abort ();
  3187.           p = handle_braces (p + 1);
  3188.           if (p == 0)
  3189.         return -1;
  3190.           /* If any args were output, mark the last one for deletion
  3191.          on failure.  */
  3192.           if (argbuf_index != index)
  3193.         record_temp_file (argbuf[argbuf_index - 1], 0, 1);
  3194.           break;
  3195.         }
  3196.  
  3197.       /* %x{OPTION} records OPTION for %X to output.  */
  3198.       case 'x':
  3199.         {
  3200.           char *p1 = p;
  3201.           char *string;
  3202.  
  3203.           /* Skip past the option value and make a copy.  */
  3204.           if (*p != '{')
  3205.         abort ();
  3206.           while (*p++ != '}')
  3207.         ;
  3208.           string = save_string (p1 + 1, p - p1 - 2);
  3209.  
  3210.           /* See if we already recorded this option.  */
  3211.           for (i = 0; i < n_linker_options; i++)
  3212.         if (! strcmp (string, linker_options[i]))
  3213.           {
  3214.             free (string);
  3215.             return 0;
  3216.           }
  3217.  
  3218.           /* This option is new; add it.  */
  3219.           n_linker_options++;
  3220.           if (!linker_options)
  3221.         linker_options
  3222.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  3223.           else
  3224.         linker_options
  3225.           = (char **) xrealloc (linker_options,
  3226.                     n_linker_options * sizeof (char **));
  3227.  
  3228.           linker_options[n_linker_options - 1] = string;
  3229.         }
  3230.         break;
  3231.  
  3232.       /* Dump out the options accumulated previously using %x,
  3233.          -Xlinker and -Wl,.  */
  3234.       case 'X':
  3235.         for (i = 0; i < n_linker_options; i++)
  3236.           {
  3237.         do_spec_1 (linker_options[i], 1, NULL_PTR);
  3238.         /* Make each accumulated option a separate argument.  */
  3239.         do_spec_1 (" ", 0, NULL_PTR);
  3240.           }
  3241.         break;
  3242.  
  3243.       /* Dump out the options accumulated previously using -Wa,.  */
  3244.       case 'Y':
  3245.         for (i = 0; i < n_assembler_options; i++)
  3246.           {
  3247.         do_spec_1 (assembler_options[i], 1, NULL_PTR);
  3248.         /* Make each accumulated option a separate argument.  */
  3249.         do_spec_1 (" ", 0, NULL_PTR);
  3250.           }
  3251.         break;
  3252.  
  3253.         /* Here are digits and numbers that just process
  3254.            a certain constant string as a spec.  */
  3255.  
  3256.       case '1':
  3257.         value = do_spec_1 (cc1_spec, 0, NULL_PTR);
  3258.         if (value != 0)
  3259.           return value;
  3260.         break;
  3261.  
  3262.       case '2':
  3263.         value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
  3264.         if (value != 0)
  3265.           return value;
  3266.         break;
  3267.  
  3268.       case 'a':
  3269.         value = do_spec_1 (asm_spec, 0, NULL_PTR);
  3270.         if (value != 0)
  3271.           return value;
  3272.         break;
  3273.  
  3274.       case 'A':
  3275.         value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
  3276.         if (value != 0)
  3277.           return value;
  3278.         break;
  3279.  
  3280.       case 'c':
  3281.         value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
  3282.         if (value != 0)
  3283.           return value;
  3284.         break;
  3285.  
  3286.       case 'C':
  3287.         value = do_spec_1 (cpp_spec, 0, NULL_PTR);
  3288.         if (value != 0)
  3289.           return value;
  3290.         break;
  3291.  
  3292.       case 'E':
  3293.         value = do_spec_1 (endfile_spec, 0, NULL_PTR);
  3294.         if (value != 0)
  3295.           return value;
  3296.         break;
  3297.  
  3298.       case 'l':
  3299.         value = do_spec_1 (link_spec, 0, NULL_PTR);
  3300.         if (value != 0)
  3301.           return value;
  3302.         break;
  3303.  
  3304.       case 'L':
  3305.         value = do_spec_1 (lib_spec, 0, NULL_PTR);
  3306.         if (value != 0)
  3307.           return value;
  3308.         break;
  3309.  
  3310.       case 'p':
  3311.         {
  3312.           char *x = (char *) alloca (strlen (cpp_predefines) + 1);
  3313.           char *buf = x;
  3314.           char *y;
  3315.  
  3316.           /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
  3317.           y = cpp_predefines;
  3318.           while (*y != 0)
  3319.         {
  3320.           if (! strncmp (y, "-D", 2))
  3321.             /* Copy the whole option.  */
  3322.             while (*y && *y != ' ' && *y != '\t')
  3323.               *x++ = *y++;
  3324.           else if (*y == ' ' || *y == '\t')
  3325.             /* Copy whitespace to the result.  */
  3326.             *x++ = *y++;
  3327.           /* Don't copy other options.  */
  3328.           else
  3329.             y++;
  3330.         }
  3331.  
  3332.           *x = 0;
  3333.  
  3334.           value = do_spec_1 (buf, 0, NULL_PTR);
  3335.           if (value != 0)
  3336.         return value;
  3337.         }
  3338.         break;
  3339.  
  3340.       case 'P':
  3341.         {
  3342.           char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
  3343.           char *buf = x;
  3344.           char *y;
  3345.  
  3346.           /* Copy all of CPP_PREDEFINES into BUF,
  3347.          but put __ after every -D and at the end of each arg.  */
  3348.           y = cpp_predefines;
  3349.           while (*y != 0)
  3350.         {
  3351.           if (! strncmp (y, "-D", 2))
  3352.             {
  3353.               int flag = 0;
  3354.  
  3355.               *x++ = *y++;
  3356.               *x++ = *y++;
  3357.  
  3358.               if (strncmp (y, "__", 2))
  3359.                 {
  3360.               /* Stick __ at front of macro name.  */
  3361.               *x++ = '_';
  3362.               *x++ = '_';
  3363.               /* Arrange to stick __ at the end as well.  */
  3364.               flag = 1;
  3365.             }
  3366.  
  3367.               /* Copy the macro name.  */
  3368.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  3369.             *x++ = *y++;
  3370.  
  3371.               if (flag)
  3372.                 {
  3373.               *x++ = '_';
  3374.               *x++ = '_';
  3375.             }
  3376.  
  3377.               /* Copy the value given, if any.  */
  3378.               while (*y && *y != ' ' && *y != '\t')
  3379.             *x++ = *y++;
  3380.             }
  3381.           else if (*y == ' ' || *y == '\t')
  3382.             /* Copy whitespace to the result.  */
  3383.             *x++ = *y++;
  3384.           /* Don't copy -A options  */
  3385.           else
  3386.             y++;
  3387.         }
  3388.           *x++ = ' ';
  3389.  
  3390.           /* Copy all of CPP_PREDEFINES into BUF,
  3391.          but put __ after every -D.  */
  3392.           y = cpp_predefines;
  3393.           while (*y != 0)
  3394.         {
  3395.           if (! strncmp (y, "-D", 2))
  3396.             {
  3397.               *x++ = *y++;
  3398.               *x++ = *y++;
  3399.  
  3400.               if (strncmp (y, "__", 2))
  3401.                 {
  3402.               /* Stick __ at front of macro name.  */
  3403.               *x++ = '_';
  3404.               *x++ = '_';
  3405.             }
  3406.  
  3407.               /* Copy the macro name.  */
  3408.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  3409.             *x++ = *y++;
  3410.  
  3411.               /* Copy the value given, if any.  */
  3412.               while (*y && *y != ' ' && *y != '\t')
  3413.             *x++ = *y++;
  3414.             }
  3415.           else if (*y == ' ' || *y == '\t')
  3416.             /* Copy whitespace to the result.  */
  3417.             *x++ = *y++;
  3418.           /* Don't copy -A options  */
  3419.           else
  3420.             y++;
  3421.         }
  3422.           *x++ = ' ';
  3423.  
  3424.           /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
  3425.           y = cpp_predefines;
  3426.           while (*y != 0)
  3427.         {
  3428.           if (! strncmp (y, "-A", 2))
  3429.             /* Copy the whole option.  */
  3430.             while (*y && *y != ' ' && *y != '\t')
  3431.               *x++ = *y++;
  3432.           else if (*y == ' ' || *y == '\t')
  3433.             /* Copy whitespace to the result.  */
  3434.             *x++ = *y++;
  3435.           /* Don't copy other options.  */
  3436.           else
  3437.             y++;
  3438.         }
  3439.  
  3440.           *x = 0;
  3441.  
  3442.           value = do_spec_1 (buf, 0, NULL_PTR);
  3443.           if (value != 0)
  3444.         return value;
  3445.         }
  3446.         break;
  3447.  
  3448.       case 'S':
  3449.         value = do_spec_1 (startfile_spec, 0, NULL_PTR);
  3450.         if (value != 0)
  3451.           return value;
  3452.         break;
  3453.  
  3454.         /* Here we define characters other than letters and digits.  */
  3455.  
  3456.       case '{':
  3457.         p = handle_braces (p);
  3458.         if (p == 0)
  3459.           return -1;
  3460.         break;
  3461.  
  3462.       case '%':
  3463.         obstack_1grow (&obstack, '%');
  3464.         break;
  3465.  
  3466.       case '*':
  3467.         do_spec_1 (soft_matched_part, 1, NULL_PTR);
  3468.         do_spec_1 (" ", 0, NULL_PTR);
  3469.         break;
  3470.  
  3471.         /* Process a string found as the value of a spec given by name.
  3472.            This feature allows individual machine descriptions
  3473.            to add and use their own specs.
  3474.            %[...] modifies -D options the way %P does;
  3475.            %(...) uses the spec unmodified.  */
  3476.       case '(':
  3477.       case '[':
  3478.         {
  3479.           char *name = p;
  3480.           struct spec_list *sl;
  3481.           int len;
  3482.  
  3483.           /* The string after the S/P is the name of a spec that is to be
  3484.          processed. */
  3485.           while (*p && *p != ')' && *p != ']')
  3486.         p++;
  3487.  
  3488.           /* See if it's in the list */
  3489.           for (len = p - name, sl = specs; sl; sl = sl->next)
  3490.         if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
  3491.           {
  3492.             name = sl->spec;
  3493.             break;
  3494.           }
  3495.  
  3496.           if (sl)
  3497.         {
  3498.           if (c == '(')
  3499.             {
  3500.               value = do_spec_1 (name, 0, NULL_PTR);
  3501.               if (value != 0)
  3502.             return value;
  3503.             }
  3504.           else
  3505.             {
  3506.               char *x = (char *) alloca (strlen (name) * 2 + 1);
  3507.               char *buf = x;
  3508.               char *y = name;
  3509.  
  3510.               /* Copy all of NAME into BUF, but put __ after
  3511.              every -D and at the end of each arg,  */
  3512.               while (1)
  3513.             {
  3514.               if (! strncmp (y, "-D", 2))
  3515.                 {
  3516.                   *x++ = '-';
  3517.                   *x++ = 'D';
  3518.                   *x++ = '_';
  3519.                   *x++ = '_';
  3520.                   y += 2;
  3521.                 }
  3522.               else if (*y == ' ' || *y == 0)
  3523.                 {
  3524.                   *x++ = '_';
  3525.                   *x++ = '_';
  3526.                   if (*y == 0)
  3527.                 break;
  3528.                   else
  3529.                 *x++ = *y++;
  3530.                 }
  3531.               else
  3532.                 *x++ = *y++;
  3533.             }
  3534.               *x = 0;
  3535.  
  3536.               value = do_spec_1 (buf, 0, NULL_PTR);
  3537.               if (value != 0)
  3538.             return value;
  3539.             }
  3540.         }
  3541.  
  3542.           /* Discard the closing paren or bracket.  */
  3543.           if (*p)
  3544.         p++;
  3545.         }
  3546.         break;
  3547.  
  3548.       case 'v':
  3549.         {
  3550.           int c1 = *p++;  /* Select first or second version number.  */
  3551.           char *p = spec_version;
  3552.           char *q, *copy;
  3553.           /* If desired, advance to second version number.  */
  3554.           if (c1 == '2')
  3555.         {
  3556.           /* Set P after the first period.  */
  3557.           while (*p != '.') p++;
  3558.           p++;
  3559.         }
  3560.           /* Set Q at the next period or at the end.  */
  3561.           q = p;
  3562.           while (*q != '.' && *q != 0) q++;
  3563.           /* Put that part into the command.  */
  3564.           obstack_grow (&obstack, p, q - p);
  3565.           arg_going = 1;
  3566.         }
  3567.         break;
  3568.  
  3569.       case '|':
  3570.         if (input_from_pipe)
  3571.           do_spec_1 ("-", 0, NULL_PTR);
  3572.         break;
  3573.  
  3574.       default:
  3575. #if 1 /* atarist */
  3576.         fprintf(stderr, "Bogus char '%c' found at pos %d of spec '%s'\n",
  3577.         c, (p - spec - 1), spec);
  3578. #endif
  3579.         abort ();
  3580.       }
  3581.     break;
  3582.  
  3583. #ifndef atarist
  3584.       /* on the atari it is important to preserve backslash in spec */
  3585.       /* so just let it go into default case */
  3586.       case '\\':
  3587.     /* Backslash: treat next character as ordinary.  */
  3588.     c = *p++;
  3589. #endif
  3590.  
  3591.     /* fall through */
  3592.       default:
  3593.     /* Ordinary character: put it into the current argument.  */
  3594.     obstack_1grow (&obstack, c);
  3595.     arg_going = 1;
  3596.       }
  3597.  
  3598.   return 0;        /* End of string */
  3599. }
  3600.  
  3601. /* Return 0 if we call do_spec_1 and that returns -1.  */
  3602.  
  3603. static char *
  3604. handle_braces (p)
  3605.      register char *p;
  3606. {
  3607.   register char *q;
  3608.   char *filter;
  3609.   int pipe = 0;
  3610.   int negate = 0;
  3611.   int suffix = 0;
  3612.  
  3613.   if (*p == '|')
  3614.     /* A `|' after the open-brace means,
  3615.        if the test fails, output a single minus sign rather than nothing.
  3616.        This is used in %{|!pipe:...}.  */
  3617.     pipe = 1, ++p;
  3618.  
  3619.   if (*p == '!')
  3620.     /* A `!' after the open-brace negates the condition:
  3621.        succeed if the specified switch is not present.  */
  3622.     negate = 1, ++p;
  3623.  
  3624.   if (*p == '.')
  3625.     /* A `.' after the open-brace means test against the current suffix.  */
  3626.     {
  3627.       if (pipe)
  3628.     abort ();
  3629.  
  3630.       suffix = 1;
  3631.       ++p;
  3632.     }
  3633.  
  3634.   filter = p;
  3635.   while (*p != ':' && *p != '}') p++;
  3636.   if (*p != '}')
  3637.     {
  3638.       register int count = 1;
  3639.       q = p + 1;
  3640.       while (count > 0)
  3641.     {
  3642.       if (*q == '{')
  3643.         count++;
  3644.       else if (*q == '}')
  3645.         count--;
  3646.       else if (*q == 0)
  3647.         abort ();
  3648.       q++;
  3649.     }
  3650.     }
  3651.   else
  3652.     q = p + 1;
  3653.  
  3654.   if (suffix)
  3655.     {
  3656.       int found = (input_suffix != 0
  3657.            && strlen (input_suffix) == p - filter
  3658.            && strncmp (input_suffix, filter, p - filter) == 0);
  3659.  
  3660.       if (p[0] == '}')
  3661.     abort ();
  3662.  
  3663.       if (negate != found
  3664.       && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  3665.     return 0;
  3666.  
  3667.       return q;
  3668.     }
  3669.   else if (p[-1] == '*' && p[0] == '}')
  3670.     {
  3671.       /* Substitute all matching switches as separate args.  */
  3672.       register int i;
  3673.       --p;
  3674.       for (i = 0; i < n_switches; i++)
  3675.     if (!strncmp (switches[i].part1, filter, p - filter))
  3676.       give_switch (i, 0);
  3677.     }
  3678.   else
  3679.     {
  3680.       /* Test for presence of the specified switch.  */
  3681.       register int i;
  3682.       int present = 0;
  3683.  
  3684.       /* If name specified ends in *, as in {x*:...},
  3685.      check for %* and handle that case.  */
  3686.       if (p[-1] == '*' && !negate)
  3687.     {
  3688.       int substitution;
  3689.       char *r = p;
  3690.  
  3691.       /* First see whether we have %*.  */
  3692.       substitution = 0;
  3693.       while (r < q)
  3694.         {
  3695.           if (*r == '%' && r[1] == '*')
  3696.         substitution = 1;
  3697.           r++;
  3698.         }
  3699.       /* If we do, handle that case.  */
  3700.       if (substitution)
  3701.         {
  3702.           /* Substitute all matching switches as separate args.
  3703.          But do this by substituting for %*
  3704.          in the text that follows the colon.  */
  3705.  
  3706.           unsigned hard_match_len = p - filter - 1;
  3707.           char *string = save_string (p + 1, q - p - 2);
  3708.  
  3709.           for (i = 0; i < n_switches; i++)
  3710.         if (!strncmp (switches[i].part1, filter, hard_match_len))
  3711.           {
  3712.             do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
  3713.             /* Pass any arguments this switch has.  */
  3714.             give_switch (i, 1);
  3715.           }
  3716.  
  3717.           return q;
  3718.         }
  3719.     }
  3720.  
  3721.       /* If name specified ends in *, as in {x*:...},
  3722.      check for presence of any switch name starting with x.  */
  3723.       if (p[-1] == '*')
  3724.     {
  3725.       for (i = 0; i < n_switches; i++)
  3726.         {
  3727.           unsigned hard_match_len = p - filter - 1;
  3728.  
  3729.           if (!strncmp (switches[i].part1, filter, hard_match_len))
  3730.         {
  3731.           switches[i].valid = 1;
  3732.           present = 1;
  3733.         }
  3734.         }
  3735.     }
  3736.       /* Otherwise, check for presence of exact name specified.  */
  3737.       else
  3738.     {
  3739.       for (i = 0; i < n_switches; i++)
  3740.         {
  3741.           if (!strncmp (switches[i].part1, filter, p - filter)
  3742.           && switches[i].part1[p - filter] == 0)
  3743.         {
  3744.           switches[i].valid = 1;
  3745.           present = 1;
  3746.           break;
  3747.         }
  3748.         }
  3749.     }
  3750.  
  3751.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  3752.      then substitute either the switch or the specified
  3753.      conditional text.  */
  3754.       if (present != negate)
  3755.     {
  3756.       if (*p == '}')
  3757.         {
  3758.           give_switch (i, 0);
  3759.         }
  3760.       else
  3761.         {
  3762.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  3763.         return 0;
  3764.         }
  3765.     }
  3766.       else if (pipe)
  3767.     {
  3768.       /* Here if a %{|...} conditional fails: output a minus sign,
  3769.          which means "standard output" or "standard input".  */
  3770.       do_spec_1 ("-", 0, NULL_PTR);
  3771.     }
  3772.     }
  3773.  
  3774.   return q;
  3775. }
  3776.  
  3777. /* Pass a switch to the current accumulating command
  3778.    in the same form that we received it.
  3779.    SWITCHNUM identifies the switch; it is an index into
  3780.    the vector of switches gcc received, which is `switches'.
  3781.    This cannot fail since it never finishes a command line.
  3782.  
  3783.    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.  */
  3784.  
  3785. static void
  3786. give_switch (switchnum, omit_first_word)
  3787.      int switchnum;
  3788.      int omit_first_word;
  3789. {
  3790.   if (!omit_first_word)
  3791.     {
  3792.       do_spec_1 ("-", 0, NULL_PTR);
  3793.       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
  3794.     }
  3795.   do_spec_1 (" ", 0, NULL_PTR);
  3796.   if (switches[switchnum].args != 0)
  3797.     {
  3798.       char **p;
  3799.       for (p = switches[switchnum].args; *p; p++)
  3800.     {
  3801.       do_spec_1 (*p, 1, NULL_PTR);
  3802.       do_spec_1 (" ", 0, NULL_PTR);
  3803.     }
  3804.     }
  3805.   switches[switchnum].valid = 1;
  3806. }
  3807.  
  3808. /* Search for a file named NAME trying various prefixes including the
  3809.    user's -B prefix and some standard ones.
  3810.    Return the absolute file name found.  If nothing is found, return NAME.  */
  3811.  
  3812. static char *
  3813. find_file (name)
  3814.      char *name;
  3815. {
  3816.   char *newname;
  3817.  
  3818.   newname = find_a_file (&startfile_prefix, name, R_OK);
  3819.   return newname ? newname : name;
  3820. }
  3821.  
  3822. /* Determine whether a directory exists.  If LINKER, return 0 for
  3823.    certain fixed names not needed by the linker.  If not LINKER, it is
  3824.    only important to return 0 if the host machine has a small ARG_MAX
  3825.    limit.  */
  3826.  
  3827. static int
  3828. is_directory (path1, path2, linker)
  3829.      char *path1;
  3830.      char *path2;
  3831.      int linker;
  3832. {
  3833.   int len1 = strlen (path1);
  3834.   int len2 = strlen (path2);
  3835.   char *path = (char *) alloca (3 + len1 + len2);
  3836.   char *cp;
  3837.   struct stat st;
  3838.  
  3839. #ifndef SMALL_ARG_MAX
  3840.   if (! linker)
  3841.     return 1;
  3842. #endif
  3843.  
  3844.   /* Construct the path from the two parts.  Ensure the string ends with "/.".
  3845.      The resulting path will be a directory even if the given path is a
  3846.      symbolic link.  */
  3847.   bcopy (path1, path, len1);
  3848.   bcopy (path2, path + len1, len2);
  3849.   cp = path + len1 + len2;
  3850.   if (cp[-1] != '/')
  3851.     *cp++ = '/';
  3852.   *cp++ = '.';
  3853.   *cp = '\0';
  3854.  
  3855.   /* Exclude directories that the linker is known to search.  */
  3856.   if (linker
  3857.       && ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
  3858.       || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0)))
  3859.     return 0;
  3860.  
  3861.   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
  3862. }
  3863.  
  3864. /* On fatal signals, delete all the temporary files.  */
  3865.  
  3866. #ifndef atarist
  3867. static void
  3868. fatal_error (signum)
  3869.      int signum;
  3870. {
  3871.   signal (signum, SIG_DFL);
  3872.   delete_failure_queue ();
  3873.   delete_temp_files ();
  3874.   /* Get the same signal again, this time not handled,
  3875.      so its normal effect occurs.  */
  3876.   kill (getpid (), signum);
  3877. }
  3878. #endif
  3879.  
  3880. int
  3881. main (argc, argv)
  3882.      int argc;
  3883.      char **argv;
  3884. {
  3885.   register int i;
  3886.   int j;
  3887.   int value;
  3888.   int linker_was_run = 0;
  3889.   char *explicit_link_files;
  3890.   char *specs_file;
  3891.   char *p;
  3892.  
  3893. #ifdef atarist
  3894. /* turn this on if you are going to set the TOS 1.4 dont clear heap flag */
  3895. /*  _malloczero(1); */     /* zero mallocs by default */
  3896.   programname = "gcc";
  3897. #else  
  3898.   p = argv[0] + strlen (argv[0]);
  3899.   while (p != argv[0] && p[-1] != '/') --p;
  3900.   programname = p;
  3901.  
  3902.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  3903.     signal (SIGINT, fatal_error);
  3904. #ifdef SIGHUP
  3905.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  3906.     signal (SIGHUP, fatal_error);
  3907. #endif
  3908.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  3909.     signal (SIGTERM, fatal_error);
  3910. #ifdef SIGPIPE
  3911.   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
  3912.     signal (SIGPIPE, fatal_error);
  3913. #endif
  3914. #endif /* atarist */
  3915.  
  3916.   argbuf_length = 10;
  3917.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  3918.  
  3919.   obstack_init (&obstack);
  3920.  
  3921. #if (!(defined(atarist) || defined(CROSSATARI)))
  3922.   /* Set up to remember the pathname of gcc and any options
  3923.      needed for collect.  We use argv[0] instead of programname because
  3924.      we need the complete pathname.  */
  3925.   obstack_init (&collect_obstack);
  3926.   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
  3927.   obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
  3928.   putenv (obstack_finish (&collect_obstack));
  3929. #endif
  3930.  
  3931.   /* Choose directory for temp files.  */
  3932.  
  3933.   choose_temp_base ();
  3934.  
  3935.   /* Make a table of what switches there are (switches, n_switches).
  3936.      Make a table of specified input files (infiles, n_infiles).
  3937.      Decode switches that are handled locally.  */
  3938.  
  3939.   process_command (argc, argv);
  3940.  
  3941. #if (defined(atarist) || defined(CROSSATARI))
  3942.   {
  3943.     char *temp;
  3944.     
  3945.     if((temp = getenv ("GCCEXEC")))
  3946.     add_prefix (&exec_prefix, temp, 0, 0, 0);
  3947.   }
  3948. #endif
  3949.   
  3950.   /* Initialize the vector of specs to just the default.
  3951.      This means one element containing 0s, as a terminator.  */
  3952.  
  3953.   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
  3954.   bcopy (default_compilers, compilers, sizeof default_compilers);
  3955.   n_compilers = n_default_compilers;
  3956.  
  3957.  
  3958. #if (defined(atarist) || defined(CROSSATARI) || defined(atariminix))
  3959.   specs_file = 0;
  3960. #else
  3961.   /* Read specs from a file if there is one.  */
  3962.  
  3963.   machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
  3964.   just_machine_suffix = concat (spec_machine, "/", "");
  3965.  
  3966.   specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
  3967.   /* Read the specs file unless it is a default one.  */
  3968.   if (specs_file != 0 && strcmp (specs_file, "specs"))
  3969.     read_specs (specs_file);
  3970. #endif
  3971.  
  3972.   /* If not cross-compiling, look for startfiles in the standard places.  */
  3973.   /* The fact that these are done here, after reading the specs file,
  3974.      means that it cannot be found in these directories.
  3975.      But that's okay.  It should never be there anyway.  */
  3976.   if (!cross_compile)
  3977.     {
  3978. #ifdef MD_EXEC_PREFIX
  3979.       add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
  3980.       add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
  3981. #endif
  3982.  
  3983. #ifdef MD_STARTFILE_PREFIX
  3984.       add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
  3985. #endif
  3986.  
  3987. #ifdef MD_STARTFILE_PREFIX_1
  3988.       add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
  3989. #endif
  3990.  
  3991.       /* If standard_startfile_prefix is relative, base it on
  3992.      standard_exec_prefix.  This lets us move the installed tree
  3993.      as a unit.  If GCC_EXEC_PREFIX is defined, base
  3994.      standard_startfile_prefix on that as well.  */
  3995.       if (*standard_startfile_prefix == '/')
  3996.     add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
  3997.             NULL_PTR);
  3998.       else
  3999.     {
  4000.       if (gcc_exec_prefix)
  4001.         add_prefix (&startfile_prefix,
  4002.             concat (gcc_exec_prefix,
  4003.                 standard_startfile_prefix,
  4004.                 ""),
  4005.             0, 0, NULL_PTR);
  4006.       add_prefix (&startfile_prefix,
  4007.               concat (standard_exec_prefix,
  4008.                   (machine_suffix)? machine_suffix : "",
  4009.                   standard_startfile_prefix),
  4010.               0, 0, NULL_PTR);
  4011.     }               
  4012.  
  4013.       add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
  4014.           NULL_PTR);
  4015.       add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
  4016.           NULL_PTR);
  4017. #if 0 /* Can cause surprises, and one can use -B./ instead.  */
  4018.       add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
  4019. #endif
  4020.     }
  4021.  
  4022.   /* Now we have the specs.
  4023.      Set the `valid' bits for switches that match anything in any spec.  */
  4024.  
  4025.   validate_all_switches ();
  4026.  
  4027.   /* Warn about any switches that no pass was interested in.  */
  4028.  
  4029.   for (i = 0; i < n_switches; i++)
  4030.     if (! switches[i].valid)
  4031.       error ("unrecognized option `-%s'", switches[i].part1);
  4032.  
  4033.   if (print_libgcc_file_name)
  4034.     {
  4035.       printf ("%s\n", find_file ("libgcc.a"));
  4036.       exit (0);
  4037.     }
  4038.  
  4039.   /* Obey some of the options.  */
  4040.  
  4041.   if (verbose_flag)
  4042.     {
  4043. #if (defined(atarist) || defined(CROSSATARI) || defined(atariminix))
  4044. #include "PatchLev.h"
  4045.       fprintf (stderr, "gcc-atariST version %s Patchlevel %s\n",
  4046.            version_string, PatchLevel);
  4047. #else
  4048.       fprintf (stderr, "gcc version %s\n", version_string);
  4049. #endif
  4050.       if (n_infiles == 0)
  4051.     exit (0);
  4052.     }
  4053.  
  4054.   if (n_infiles == 0)
  4055.     fatal ("No input files");
  4056.  
  4057.   /* Make a place to record the compiler output file names
  4058.      that correspond to the input files.  */
  4059.  
  4060.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  4061.   bzero (outfiles, n_infiles * sizeof (char *));
  4062.  
  4063.   /* Record which files were specified explicitly as link input.  */
  4064.  
  4065.   explicit_link_files = xmalloc (n_infiles);
  4066.   bzero (explicit_link_files, n_infiles);
  4067.  
  4068.   for (i = 0; i < n_infiles; i++)
  4069.     {
  4070.       register struct compiler *cp = 0;
  4071.       int this_file_error = 0;
  4072.  
  4073.       /* Tell do_spec what to substitute for %i.  */
  4074.  
  4075.       input_filename = infiles[i].name;
  4076.       input_filename_length = strlen (input_filename);
  4077.       input_file_number = i;
  4078.  
  4079.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  4080.  
  4081.       outfiles[i] = input_filename;
  4082.  
  4083.       /* Figure out which compiler from the file's suffix.  */
  4084.  
  4085.       cp = lookup_compiler (infiles[i].name, input_filename_length,
  4086.                 infiles[i].language);
  4087.  
  4088.       if (cp)
  4089.     {
  4090.       /* Ok, we found an applicable compiler.  Run its spec.  */
  4091.       /* First say how much of input_filename to substitute for %b  */
  4092.       register char *p;
  4093.       int len;
  4094.  
  4095.       input_basename = input_filename;
  4096.       for (p = input_filename; *p; p++)
  4097. #ifdef atarist
  4098.         if (((*p == '/') || (*p == '\\')) && (*(p-1) != ':'))
  4099.             /* allow both '\' and '/' with  our new lib */
  4100. #else
  4101.         if (*p == '/')
  4102. #endif
  4103.           input_basename = p + 1;
  4104.  
  4105.       /* Find a suffix starting with the last period,
  4106.          and set basename_length to exclude that suffix.  */
  4107.       basename_length = strlen (input_basename);
  4108.       p = input_basename + basename_length;
  4109.       while (p != input_basename && *p != '.') --p;
  4110.       if (*p == '.' && p != input_basename)
  4111.         {
  4112.           basename_length = p - input_basename;
  4113.           input_suffix = p + 1;
  4114.         }
  4115.       else
  4116.         input_suffix = "";
  4117.  
  4118.       len = 0;
  4119.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  4120.         if (cp->spec[j])
  4121.           len += strlen (cp->spec[j]);
  4122.  
  4123.       p = (char *) xmalloc (len + 1);
  4124.  
  4125.       len = 0;
  4126.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  4127.         if (cp->spec[j])
  4128.           {
  4129.         strcpy (p + len, cp->spec[j]);
  4130.         len += strlen (cp->spec[j]);
  4131.           }
  4132.  
  4133.       value = do_spec (p);
  4134.       free (p);
  4135.       if (value < 0)
  4136.         this_file_error = 1;
  4137.     }
  4138.  
  4139.       /* If this file's name does not contain a recognized suffix,
  4140.      record it as explicit linker input.  */
  4141.  
  4142.       else
  4143.     explicit_link_files[i] = 1;
  4144.  
  4145.       /* Clear the delete-on-failure queue, deleting the files in it
  4146.      if this compilation failed.  */
  4147.  
  4148.       if (this_file_error)
  4149.     {
  4150.       delete_failure_queue ();
  4151.       error_count++;
  4152.     }
  4153.       /* If this compilation succeeded, don't delete those files later.  */
  4154.       clear_failure_queue ();
  4155.     }
  4156.  
  4157.   /* Run ld to link all the compiler output files.  */
  4158.  
  4159.   if (error_count == 0)
  4160.     {
  4161.       int tmp = execution_count;
  4162.       int i;
  4163.       int first_time;
  4164.  
  4165. #if (!(defined(atarist) || defined(CROSSATARI)))
  4166.       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
  4167.      for collect.  */
  4168.       putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
  4169.       putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
  4170.  
  4171.       /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
  4172.      the compiler.  */
  4173.       obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
  4174.             sizeof ("COLLECT_GCC_OPTIONS=")-1);
  4175.  
  4176.       first_time = TRUE;
  4177.       for (i = 0; i < n_switches; i++)
  4178.     {
  4179.       char **args;
  4180.       if (!first_time)
  4181.         obstack_grow (&collect_obstack, " ", 1);
  4182.  
  4183.       first_time = FALSE;
  4184.       obstack_grow (&collect_obstack, "-", 1);
  4185.       obstack_grow (&collect_obstack, switches[i].part1,
  4186.             strlen (switches[i].part1));
  4187.  
  4188.       for (args = switches[i].args; args && *args; args++)
  4189.         {
  4190.           obstack_grow (&collect_obstack, " ", 1);
  4191.           obstack_grow (&collect_obstack, *args, strlen (*args));
  4192.         }
  4193.     }
  4194.       obstack_grow (&collect_obstack, "\0", 1);
  4195.       putenv (obstack_finish (&collect_obstack));
  4196. #endif
  4197.  
  4198.       value = do_spec (link_command_spec);
  4199.       if (value < 0)
  4200.     error_count = 1;
  4201.       linker_was_run = (tmp != execution_count);
  4202.     }
  4203.  
  4204.   /* Warn if a -B option was specified but the prefix was never used.  */
  4205.   unused_prefix_warnings (&exec_prefix);
  4206.   unused_prefix_warnings (&startfile_prefix);
  4207.  
  4208.   /* If options said don't run linker,
  4209.      complain about input files to be given to the linker.  */
  4210.  
  4211.   if (! linker_was_run && error_count == 0)
  4212.     for (i = 0; i < n_infiles; i++)
  4213.       if (explicit_link_files[i])
  4214.     error ("%s: linker input file unused since linking not done",
  4215.            outfiles[i]);
  4216.  
  4217.   /* Delete some or all of the temporary files we made.  */
  4218.  
  4219.   if (error_count)
  4220.     delete_failure_queue ();
  4221.   delete_temp_files ();
  4222.  
  4223.   exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
  4224.   /* NOTREACHED */
  4225.   return 0;
  4226. }
  4227.  
  4228. /* Find the proper compilation spec for the file name NAME,
  4229.    whose length is LENGTH.  LANGUAGE is the specified language,
  4230.    or 0 if none specified.  */
  4231.  
  4232. static struct compiler *
  4233. lookup_compiler (name, length, language)
  4234.      char *name;
  4235.      int length;
  4236.      char *language;
  4237. {
  4238.   struct compiler *cp;
  4239.  
  4240.   /* Look for the language, if one is spec'd.  */
  4241.   if (language != 0)
  4242.     {
  4243.       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  4244.     {
  4245.       if (language != 0)
  4246.         {
  4247.           if (cp->suffix[0] == '@'
  4248.           && !strcmp (cp->suffix + 1, language))
  4249.         return cp;
  4250.         }
  4251.     }
  4252.       error ("language %s not recognized", language);
  4253.     }
  4254.  
  4255.   /* Look for a suffix.  */
  4256.   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  4257.     {
  4258.       if (/* The suffix `-' matches only the file name `-'.  */
  4259.       (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
  4260.       ||
  4261.       (strlen (cp->suffix) < length
  4262.        /* See if the suffix matches the end of NAME.  */
  4263.        && !strcmp (cp->suffix,
  4264.                name + length - strlen (cp->suffix))))
  4265.     {
  4266.       if (cp->spec[0][0] == '@')
  4267.         {
  4268.           struct compiler *new;
  4269.           /* An alias entry maps a suffix to a language.
  4270.          Search for the language; pass 0 for NAME and LENGTH
  4271.          to avoid infinite recursion if language not found.
  4272.          Construct the new compiler spec.  */
  4273.           language = cp->spec[0] + 1;
  4274.           new = (struct compiler *) xmalloc (sizeof (struct compiler));
  4275.           new->suffix = cp->suffix;
  4276.           bcopy (lookup_compiler (NULL_PTR, 0, language)->spec,
  4277.              new->spec, sizeof new->spec);
  4278.           return new;
  4279.         }
  4280.       /* A non-alias entry: return it.  */
  4281.       return cp;
  4282.     }
  4283.     }
  4284.  
  4285.   return 0;
  4286. }
  4287.  
  4288. char *
  4289. xmalloc (size)
  4290.      unsigned size;
  4291. {
  4292.   register char *value = (char *) malloc (size);
  4293.   if (value == 0)
  4294.     fatal ("virtual memory exhausted");
  4295.   return value;
  4296. }
  4297.  
  4298. char *
  4299. xrealloc (ptr, size)
  4300.      char *ptr;
  4301.      unsigned size;
  4302. {
  4303.   register char *value = (char *) realloc (ptr, size);
  4304.   if (value == 0)
  4305.     fatal ("virtual memory exhausted");
  4306.   return value;
  4307. }
  4308.  
  4309. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  4310.  
  4311. static char *
  4312. concat (s1, s2, s3)
  4313.      char *s1, *s2, *s3;
  4314. {
  4315.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4316.   char *result = xmalloc (len1 + len2 + len3 + 1);
  4317.  
  4318.   strcpy (result, s1);
  4319.   strcpy (result + len1, s2);
  4320.   strcpy (result + len1 + len2, s3);
  4321.   *(result + len1 + len2 + len3) = 0;
  4322.  
  4323.   return result;
  4324. }
  4325.  
  4326. static char *
  4327. save_string (s, len)
  4328.      char *s;
  4329.      int len;
  4330. {
  4331.   register char *result = xmalloc (len + 1);
  4332.  
  4333.   bcopy (s, result, len);
  4334.   result[len] = 0;
  4335.   return result;
  4336. }
  4337.  
  4338. static void
  4339. pfatal_with_name (name)
  4340.      char *name;
  4341. {
  4342.   char *s;
  4343.  
  4344.   if (errno < sys_nerr)
  4345.     s = concat ("%s: ", sys_errlist[errno], "");
  4346.   else
  4347.     s = "cannot open %s";
  4348.   fatal (s, name);
  4349. }
  4350.  
  4351. static void
  4352. perror_with_name (name)
  4353.      char *name;
  4354. {
  4355.   char *s;
  4356.  
  4357.   if (errno < sys_nerr)
  4358.     s = concat ("%s: ", sys_errlist[errno], "");
  4359.   else
  4360.     s = "cannot open %s";
  4361.   error (s, name);
  4362. }
  4363.  
  4364. static void
  4365. perror_exec (name)
  4366.      char *name;
  4367. {
  4368.   char *s;
  4369.  
  4370.   if (errno < sys_nerr)
  4371.     s = concat ("installation problem, cannot exec %s: ",
  4372.         sys_errlist[errno], "");
  4373.   else
  4374.     s = "installation problem, cannot exec %s";
  4375.   error (s, name);
  4376. }
  4377.  
  4378. /* More 'friendly' abort that prints the line and file.
  4379.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  4380.  
  4381. void
  4382. fancy_abort ()
  4383. {
  4384.   fatal ("Internal gcc abort.");
  4385. }
  4386.  
  4387. #ifdef HAVE_VPRINTF
  4388.  
  4389. /* Output an error message and exit */
  4390.  
  4391. static void
  4392. fatal (va_alist)
  4393.      va_dcl
  4394. {
  4395.   va_list ap;
  4396.   char *format;
  4397.  
  4398.   va_start (ap);
  4399.   format = va_arg (ap, char *);
  4400.   fprintf (stderr, "%s: ", programname);
  4401.   vfprintf (stderr, format, ap);
  4402.   va_end (ap);
  4403.   fprintf (stderr, "\n");
  4404.   delete_temp_files ();
  4405.   exit (1);
  4406. }
  4407.  
  4408. static void
  4409. error (va_alist)
  4410.      va_dcl
  4411. {
  4412.   va_list ap;
  4413.   char *format;
  4414.  
  4415.   va_start (ap);
  4416.   format = va_arg (ap, char *);
  4417.   fprintf (stderr, "%s: ", programname);
  4418.   vfprintf (stderr, format, ap);
  4419.   va_end (ap);
  4420.  
  4421.   fprintf (stderr, "\n");
  4422. }
  4423.  
  4424. #else /* not HAVE_VPRINTF */
  4425.  
  4426. static void
  4427. fatal (msg, arg1, arg2)
  4428.      char *msg, *arg1, *arg2;
  4429. {
  4430.   error (msg, arg1, arg2);
  4431.   delete_temp_files ();
  4432.   exit (1);
  4433. }
  4434.  
  4435. static void
  4436. error (msg, arg1, arg2)
  4437.      char *msg, *arg1, *arg2;
  4438. {
  4439.   fprintf (stderr, "%s: ", programname);
  4440.   fprintf (stderr, msg, arg1, arg2);
  4441.   fprintf (stderr, "\n");
  4442. }
  4443.  
  4444. #endif /* not HAVE_VPRINTF */
  4445.  
  4446.  
  4447. static void
  4448. validate_all_switches ()
  4449. {
  4450.   struct compiler *comp;
  4451.   register char *p;
  4452.   register char c;
  4453.   struct spec_list *spec;
  4454.  
  4455.   for (comp = compilers; comp->spec[0]; comp++)
  4456.     {
  4457.       int i;
  4458.       for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
  4459.     {
  4460.       p = comp->spec[i];
  4461.       while (c = *p++)
  4462.         if (c == '%' && *p == '{')
  4463.           /* We have a switch spec.  */
  4464.           validate_switches (p + 1);
  4465.     }
  4466.     }
  4467.  
  4468.   /* look through the linked list of extra specs read from the specs file */
  4469.   for (spec = specs; spec ; spec = spec->next)
  4470.     {
  4471.       p = spec->spec;
  4472.       while (c = *p++)
  4473.     if (c == '%' && *p == '{')
  4474.       /* We have a switch spec.  */
  4475.       validate_switches (p + 1);
  4476.     }
  4477.  
  4478.   p = link_command_spec;
  4479.   while (c = *p++)
  4480.     if (c == '%' && *p == '{')
  4481.       /* We have a switch spec.  */
  4482.       validate_switches (p + 1);
  4483.  
  4484.   /* Now notice switches mentioned in the machine-specific specs.  */
  4485.  
  4486.   p = asm_spec;
  4487.   while (c = *p++)
  4488.     if (c == '%' && *p == '{')
  4489.       /* We have a switch spec.  */
  4490.       validate_switches (p + 1);
  4491.  
  4492.   p = asm_final_spec;
  4493.   while (c = *p++)
  4494.     if (c == '%' && *p == '{')
  4495.       /* We have a switch spec.  */
  4496.       validate_switches (p + 1);
  4497.  
  4498.   p = cpp_spec;
  4499.   while (c = *p++)
  4500.     if (c == '%' && *p == '{')
  4501.       /* We have a switch spec.  */
  4502.       validate_switches (p + 1);
  4503.  
  4504.   p = signed_char_spec;
  4505.   while (c = *p++)
  4506.     if (c == '%' && *p == '{')
  4507.       /* We have a switch spec.  */
  4508.       validate_switches (p + 1);
  4509.  
  4510.   p = cc1_spec;
  4511.   while (c = *p++)
  4512.     if (c == '%' && *p == '{')
  4513.       /* We have a switch spec.  */
  4514.       validate_switches (p + 1);
  4515.  
  4516.   p = cc1plus_spec;
  4517.   while (c = *p++)
  4518.     if (c == '%' && *p == '{')
  4519.       /* We have a switch spec.  */
  4520.       validate_switches (p + 1);
  4521.  
  4522.   p = link_spec;
  4523.   while (c = *p++)
  4524.     if (c == '%' && *p == '{')
  4525.       /* We have a switch spec.  */
  4526.       validate_switches (p + 1);
  4527.  
  4528.   p = lib_spec;
  4529.   while (c = *p++)
  4530.     if (c == '%' && *p == '{')
  4531.       /* We have a switch spec.  */
  4532.       validate_switches (p + 1);
  4533.  
  4534.   p = startfile_spec;
  4535.   while (c = *p++)
  4536.     if (c == '%' && *p == '{')
  4537.       /* We have a switch spec.  */
  4538.       validate_switches (p + 1);
  4539. }
  4540.  
  4541. /* Look at the switch-name that comes after START
  4542.    and mark as valid all supplied switches that match it.  */
  4543.  
  4544. static void
  4545. validate_switches (start)
  4546.      char *start;
  4547. {
  4548.   register char *p = start;
  4549.   char *filter;
  4550.   register int i;
  4551.   int suffix = 0;
  4552.  
  4553.   if (*p == '|')
  4554.     ++p;
  4555.  
  4556.   if (*p == '!')
  4557.     ++p;
  4558.  
  4559.   if (*p == '.')
  4560.     suffix = 1, ++p;
  4561.  
  4562.   filter = p;
  4563.   while (*p != ':' && *p != '}') p++;
  4564.  
  4565.   if (suffix)
  4566.     ;
  4567.   else if (p[-1] == '*')
  4568.     {
  4569.       /* Mark all matching switches as valid.  */
  4570.       --p;
  4571.       for (i = 0; i < n_switches; i++)
  4572.     if (!strncmp (switches[i].part1, filter, p - filter))
  4573.       switches[i].valid = 1;
  4574.     }
  4575.   else
  4576.     {
  4577.       /* Mark an exact matching switch as valid.  */
  4578.       for (i = 0; i < n_switches; i++)
  4579.     {
  4580.       if (!strncmp (switches[i].part1, filter, p - filter)
  4581.           && switches[i].part1[p - filter] == 0)
  4582.         switches[i].valid = 1;
  4583.     }
  4584.     }
  4585. }
  4586.